Google-Developer-Student-Club-CCOEW / Competitive-Programming-2023-GDSC-CUMMINS-X-GDSC-MMCOE

Welcome to the Hacktoberfest 2023 Competitive-Programming Cohort for Cummins College and MMCOE students! To request issue assignment, create a pull request, providing: 1. Full Name 🧑‍🎓 2.Email 📧 3.College ID (RNO) 🔢 4.Branch of Study.📚 5. Year 📆 .The Cummins College and MMCOE students' PRs will be considered only. Thank you!
9 stars 69 forks source link

Find the Difference #27

Open saakshii12 opened 1 year ago

saakshii12 commented 1 year ago

Write code in the preferred language and attach output with it.

You are given two strings s and t.

String t is generated by random shuffling string s and then add one more letter at a random position.

Return the letter that was added to t.

Example 1:

Input: s = "abcd", t = "abcde" Output: "e" Explanation: 'e' is the letter that was added.

Example 2:

Input: s = "", t = "y" Output: "y"

maskboyAvi commented 1 year ago

@saakshii12 I want to solve this issue. Please assign it to me.

sharwaritijare commented 1 year ago

@saakshii12 Can you please assign me this issue Sharwari FY Cummins college of engineering

smitkevadiya50 commented 11 months ago

def find_added_letter(s, t):

Convert the strings into lists of characters

s_list = list(s)
t_list = list(t)

# Sort both lists to align similar characters
s_list.sort()
t_list.sort()

# Compare the two lists
for i in range(len(s_list)):
    if s_list[i] != t_list[i]:
        return t_list[i]

# If all the characters in s_list are matched, the last character in t_list is the added one
return t_list[-1]

Test the function with the provided examples

example1 = find_added_letter("abcd", "abcde") example2 = find_added_letter("", "y")

print(example1) print(example2)