You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.regex.*;
public class Solution {
// Complete the makeAnagram function below. static int makeAnagram(String a, String b) { String joined = (a + b).chars().distinct().sorted() .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString(); int count = 0; int countCharA; int countCharB; for (int i = 0; i < joined.length(); i++) { countCharA = 0; countCharB = 0; final char charAt = joined.charAt(i); if (a.contains(String.valueOf(joined.charAt(i))) && b.contains(String.valueOf(joined.charAt(i)))) { countCharA += a.chars().filter(c -> (char) c == charAt).count(); countCharB += b.chars().filter(c -> (char) c == charAt).count(); count += countCharA == countCharB ? countCharA+countCharB : countCharA > countCharB ? countCharB*2 : countCharA*2; } } return (a.length()+b.length())-count; } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); String a = scanner.nextLine(); String b = scanner.nextLine(); int res = makeAnagram(a, b); bufferedWriter.write(String.valueOf(res)); bufferedWriter.newLine(); bufferedWriter.close(); scanner.close(); }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Strings: Making Anagrams
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.regex.*;
public class Solution {
}