问题
Given two strings, a and b, that may or may not be of the same length, determine the minimum number of character deletions required to make a and b anagrams. Any characters can be deleted from either of the strings.
This is my first time preparing for competitive programming and understanding the logic behind the two for loops is quite hard for me.
String str1 = s.next();
String str2 = s.next();
char []c1 = str1.toCharArray();
char []c2 = str2.toCharArray();
int []cnt1 = new int[26];
int []cnt2 = new int[26];
int len1 = str1.length();
for (int i = 0; i < len1; i++) {
cnt1[c1[i] - 97]++;
}
int len2 = str2.length();
for (int i = 0; i < len2; i++) {
cnt2[c2[i] - 97]++;
}
int cnt = 0;
for (int i = 0; i < 26; i++) {
cnt += Math.abs(cnt2[i] - cnt1[i]);
}
System.out.println(cnt);
回答1:
This snippet goes over each string and counts the number of occurrences each letter has in it (and store the counters in an array for better performance).
It then goes over the two arrays of counters, and for each letter subtracts the counters for both strings (in absolute value). The difference is the number of that character that should be removed. These differences are summed, and the result is the answer.
回答2:
Okay, this is what the program is doing with two for loops.
Imagine 'cnt1' as English alphabet 'A' to 'Z' written up on a paper from left to right, and so is 'cnt2'. First for loop is tick marking a letter on the paper should it be found in 'string1', and so does second for 'string2'.
Now, you have two papers with 'A' to 'Z' written up on them from left to right, and after two 'for loops' have executed, each of the paper has tick marks on those letters that were present in respective string inputs.
Now, if a letter is ticked on both the papers, leave it alone, and should you find any of the letter ticked in one paper (i.e. in the array), and is not ticked in the other array, then count it as a letter to be deleted.
By the time you have scanned both the papers like this from left to right, you'd have number of letters that need to be removed in totality from both the papers.
Lets see how it is implemented in code. Default initial values of primitive array is all zeros, and the act of 'tick marking' a letter on paper is achieved by changing that particular index to '1'.
So, by the time the first two for loops end, each of 'cnt1' and 'cnt2' arrays would have '1' in it randomly. If both the arrays have '1' or '0' in it for a given index, you need not count it, should they be different i.e. difference of that particular index for both the arrays is '1' (that's why you see Math.abs being used), then that is a letter to be deleted from first string, or the second.
edit: For competitive exams, you should be able to visualize the solution first, and then find an optimum one. Computers only add speed to the solution found. They don't think, we make them think :)
Hope you could visualize the solution first and are still getting used to programing. All the best!
来源:https://stackoverflow.com/questions/56815990/cant-understand-the-logic-behind-this-anagram-problems-solution