Created
May 13, 2016 14:52
-
-
Save scottopell/aed5498144d88359e24f0ec050fdb3a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
public class Solution { | |
static boolean isAnagram(String A, String B) { | |
//Complete the function | |
if (A.length() != B.length()) | |
return false; | |
Hashtable<Character, Integer> H = new Hashtable<Character, Integer>(); | |
// iterate through A | |
for (int i=0; i<A.length(); i++) { | |
char c = A.charAt(i); | |
if (H.get(c) != null) { | |
H.put(c, H.get(c)+1); | |
} else { | |
H.put(c, 1); | |
} | |
} | |
// iterate through B | |
for (int j=0; j<B.length(); j++) { | |
char c = A.charAt(j); | |
if (H.get(c) != null) { | |
if (H.get(c).equals(1)) { | |
H.remove(c); | |
} else { | |
H.put(c, H.get(c)-1); | |
} | |
} else { | |
H.put(c, 1); | |
} | |
} | |
System.out.println(H.toString()); | |
System.out.println(H.isEmpty()); | |
if (H.isEmpty()) return true; | |
else return false; | |
} | |
public static void main(String[] args) { | |
Scanner sc=new Scanner(System.in); | |
String A=sc.next(); | |
String B=sc.next(); | |
boolean ret=isAnagram(A,B); | |
if(ret)System.out.println("Anagrams"); | |
else System.out.println("Not Anagrams"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment