Write a C program : String Permutations You are given two strings 'X' and 'Y, each containing at most 1,000 character. Write a program that can determine whether the characters in the first string x can be rearranged to form the second string 'Y'. The output should be "yes" this is possible and "no" if not. Input Specification: input1: the string 'X' input2: the string 'Y' Output Specification: Return "yes" or "no" accordingly. Example 1: input1: zbk input2: zkb Output: yes Explanation: You can rearrange zbk to be zkb (by switching the k and the b). Hence the output is "Yes". Example 2: input1: Mettl input2: Coding Output: no.
Answers
Answered by
0
Explanation:
as metti cannot be formed from rearranging "coding".Hence the output is "no"
Answered by
1
Given below is the python program and algorithm used to carry out the given function.
Explanation:
- Declare give inputs to the two strings 'X' and 'Y' that are to compared.
- Declare an array 'count' that contains the frequencies of the of characters from string 'X'.
- Run a loop on string 'Y' to check if every character of 'Y' has matching frequencies to that of string 'X' using the array 'count'.
- If the frequencies are matching than print 'yes' and else print 'No'.
- def canMakeStr2(s1, s2):
- count = {s1[i] : 0 for i in range(len(s1))}
- for i in range(len(s1)):
- count[s1[i]] += 1
- for i in range(len(s2)):
- if (count.get(s2[i]) == None or count[s2[i]] == 0):
- return False
- count[s2[i]] -= 1
- return True
- X = input("Enter first string :")
- Y = input("Enter second string :")
- if canMakeStr2(X, Y):
- print("Yes")
- else:
- print("No")
Similar questions
Hindi,
4 hours ago
English,
7 hours ago
Computer Science,
7 hours ago
Math,
8 months ago
Math,
8 months ago