Вы должны проверить, сколько символов повторяется в обеих строках.Пожалуйста, проверьте, работает ли приведенная ниже логика
1. Declare two arrays of size equal to the length of the string (suppose N). (The length
of both the strings must be equal first of all). This array stores the number of
characters which are repeated once,twice,thrice...N times
2. Traverse the strings and calculate the frequency of each character. Sorting the
strings beforehand might help.
3. On getting the frequency of one character in a particular string, update the array
corresponding to that string.
4. Set array[newly found frequency] += 1;
5. Repeat steps 3-4 for both the strings. After this is done for both the strings, we
have two arrays containing the information about how many characters are repeated how
many times in both the strings.
6. Next step is to match both the arrays. If they match return True else False.
Я объясню вышеупомянутые шаги на вашем примере:
Source: ABA
Dest: BAB
Arrays would be: arr1[3]={0,0,0} and arr2[3]={0,0,0}
(storing nummber of characters with frequency n in arr[n-1])
In string 1: Characters with 2 repetitions: 1 (A) so- arr1[2-1]+=1;
Characters with 1 repetition: 1 (B) so- arr1[1-1]+=1;
arr1= {1,1,0}
In string 2: Characters with 2 repetitions: 1 (B) so- arr2[2-1]+=1;
Characters with 1 repetition: 1 (A) so- arr2[1-1]+=1;
arr2= {1,1,0}
As arr1 and arr2 match, the strings are convertible.
Надеюсь, это поможет:)