是否有一系列转换步骤将给定的源字符串转换为目标字符串?
注意:在每个步骤中,可以转换一个字符(替换应该是'A'-'Z'之间的字符),并且必须转换该字符的所有出现位置。 示例如下。
Source: "ABA"
Dest: "BAB"
Output: True
Explanation: ABA -> A**C**A -> **B**C**B** -> B**A**B
Source: "AA"
Dest: "BC"
Output: False
Explanation: AA -> BB
我认为HashMap
有用。但是还有另一个数据结构,我不打算逐个字符地进行比较吗?
答案 0 :(得分:1)
您可以尝试以下方法:
when: vdom|default("")|length == 0
答案 1 :(得分:0)
如果两个字符串都应具有相同的长度和相同的字符(与我们要转换的字符不同),则返回true。
请检查以下方法是否有效:
答案 2 :(得分:0)
基本上,您必须检查两个字符串中是否重复了多少个字符。请检查以下逻辑是否有效
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.
希望这会有所帮助:)