Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class Solution {
- static boolean isAnagram(String a, String b) {
- Map<Character, Integer> dictionary = new HashMap<Character, Integer>();
- a = a.toLowerCase();
- b = b.toLowerCase();
- for (int i = 0; i < a.length(); i++) {
- if(!dictionary.containsKey(a.charAt(i))){
- dictionary.put(a.charAt(i), 1);
- } else {
- dictionary.replace(a.charAt(i), dictionary.get(a.charAt(i)) + 1);
- }
- }
- for (int y = 0; y < b.length(); y++) {
- if(!dictionary.containsKey(b.charAt(y))){
- return false;
- } else {
- dictionary.replace(b.charAt(y), dictionary.get(b.charAt(y)) - 1);
- }
- }
- for(Map.Entry<Character, Integer> entry: dictionary.entrySet()){
- if(entry.getValue()!=0){
- return false;
- }
- }
- return true;
- }
- public static void main(String[] args){
- Scanner scanner = new Scanner(System.in);
- String a = scanner.nextLine();
- String b = scanner.nextLine();
- scanner.close();
- System.out.println(isAnagram(a, b)?"Anagrams":"Not Anagrams");
- }
- }
Add Comment
Please, Sign In to add comment