package com.fishercoder.solutions;

import java.util.Arrays;

/**Given two strings s and t, write a function to determine if t is an anagram of s.

 For example,
 s = "anagram", t = "nagaram", return true.
 s = "rat", t = "car", return false.

 Note:
 You may assume the string contains only lowercase alphabets.

 Follow up:
 What if the inputs contain unicode characters? How would you adapt your solution to such case?*/

public class _242 {
    public boolean isAnagram_solution1(String s, String t) {
        char[] schar = s.toCharArray();
        char[] tchar = t.toCharArray();
        Arrays.sort(schar);
        Arrays.sort(tchar);
        return new String(schar).equals(new String(tchar));
    }
    
    //another way: although much slower
    public boolean isAnagram_solution2(String s, String t) {
        if(s == null || t == null || s.length() != t.length()) return false;
        int[] counts = new int[26];
        for(int i = 0; i < s.length(); i++){
            counts[s.charAt(i) - 'a']++;
            counts[t.charAt(i) - 'a']--;
        }
        for(int i : counts){
            if(i != 0) return false;
        }
        return true;
    }
}

@@ -58,16 +58,42 @@
 ys;%0A%0A/**
+%0A * 242. Valid Anagram%0A * 
 Given tw
@@ -440,16 +440,18 @@
 ch case?
+%0A 
 */%0A%0Apubl
@@ -466,16 +466,63 @@
  _242 %7B%0A
+%0A    public static class SortingSolution %7B%0A    
     publ
@@ -545,18 +545,8 @@
 gram
-_solution1
 (Str
@@ -556,32 +556,36 @@
  s, String t) %7B%0A
+    
         char%5B%5D s
@@ -612,24 +612,28 @@
 ();%0A        
+  
+  
 char%5B%5D tchar
@@ -652,32 +652,36 @@
 rray();%0A        
+    
 Arrays.sort(scha
@@ -684,32 +684,36 @@
 schar);%0A        
+ 
+   
 Arrays.sort(tcha
@@ -720,24 +720,28 @@
 r);%0A        
+   
+ 
 return new S
@@ -780,16 +780,20 @@
 char));%0A
+    
     %7D%0A  
@@ -798,49 +798,58 @@
     
-%0A    //another way: although much slower%0A
+%7D%0A%0A    public static class CountingSolution %7B%0A    
     
@@ -876,18 +876,8 @@
 gram
-_solution2
 (Str
@@ -887,32 +887,36 @@
  s, String t) %7B%0A
+    
         if(s == 
@@ -909,16 +909,17 @@
       if
+ 
 (s == nu
@@ -985,16 +985,20 @@
         
+    
 int%5B%5D co
@@ -1021,27 +1021,32 @@
 6%5D;%0A        
+  
+  
 for
+ 
 (int i = 0; 
@@ -1065,16 +1065,17 @@
 (); i++)
+ 
 %7B%0A      
@@ -1076,24 +1076,28 @@
             
+  
+  
 counts%5Bs.cha
@@ -1113,16 +1113,20 @@
 'a'%5D++;%0A
+    
         
@@ -1166,26 +1166,33 @@
         
-%7D%0A
+    %7D%0A   
         
 for(int 
@@ -1183,19 +1183,21 @@
         
+ 
 for
+ 
 (int i :
@@ -1204,18 +1204,23 @@
  counts)
+ 
 %7B%0A
+    
         
@@ -1225,16 +1225,17 @@
       if
+ 
 (i != 0)
@@ -1249,32 +1249,36 @@
  false;%0A        
+    
 %7D%0A        return
@@ -1267,24 +1267,28 @@
   %7D%0A        
+ 
+   
 return true;
@@ -1288,16 +1288,26 @@
 n true;%0A
+        %7D%0A
     %7D%0A%7D%0A

edit 242