You are viewing a single comment's thread. Return to all comments →
Scanner sc = new Scanner(System.in); String a = sc.next(); String b = sc.next(); HashMap<String, Integer> mapA = new HashMap<>(); HashMap<String, Integer> mapB = new HashMap<>(); for (int i = 0; i < a.length(); i++) { String letter = String.valueOf(a.charAt(i)).toLowerCase(); if(mapA.containsKey(letter)){ Integer sum = mapA.get(letter); mapA.put(letter, sum += 1); }else{ mapA.put(letter, 1); } } for (int i = 0; i < b.length(); i++) { String letter = String.valueOf(b.charAt(i)).toLowerCase(); if(mapB.containsKey(letter)){ Integer sum = mapB.get(letter); mapB.put(letter, sum += 1); }else{ mapB.put(letter, 1); } } boolean isAnagrams = true; for(Map.Entry entry : mapA.entrySet()){ if(entry.getValue() != mapB.get(entry.getKey()) || mapA.size() != mapB.size()){ isAnagrams = false; } } System.out.println(isAnagrams ? "Anagrams" : "Not Anagrams");
Seems like cookies are disabled on this browser, please enable them to open this website
Java Anagrams
You are viewing a single comment's thread. Return to all comments →