Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static List<Integer> minCharactersForAnagrams(List<String> a, List<String> b) {
- List<Integer> result = new ArrayList<>();
- for (int i = 0; i < a.size(); i++) {
- String strA = a.get(i);
- String strB = b.get(i);
- if (strA.length() != strB.length()) {
- result.add(-1);
- } else {
- int[] count = new int[26]; // Assuming only lowercase English letters
- for (int j = 0; j < strA.length(); j++) {
- count[strA.charAt(j) - 'a']++;
- count[strB.charAt(j) - 'a']--;
- }
- int modifications = 0;
- for (int value : count) {
- modifications += Math.abs(value);
- }
- result.add(modifications / 2);
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement