Minimum Number of Steps to Make Two Strings Anagram
给两个字符串s和t, 每一步可以replace一个字符, 问多少步可以把t变成s的anagram. anagram的题肯定要先count. 然后因为每次取代都是remove一个, add一个, 所以结果要除以2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Solution { public int minSteps(String s, String t) { int[] count_s = new int[26]; int[] count_t = new int[26]; for(int i = 0; i < s.length(); i++) { count_s[s.charAt(i) - 'a']++; } for(int i = 0; i < t.length(); i++) { count_t[t.charAt(i) - 'a']++; } int res = 0; for(int i = 0; i < 26; i++) { res += Math.abs(count_s[i] - count_t[i]); } return res / 2; } } |