Java O(n) with 3ms solution


  • 0
    B

    This solution with 256 bucket would cover unicode.

        public boolean isAnagram(String s, String t) {
    	    int[] hash = new int[256];
    	    int count = 0;
    	
    	    if(s.length()!= t.length())
    	        return false;
    	        
    	    for(char c : s.toCharArray()){
    	        hash[c]++;
    	    }
    	    
    	    for(char c2: t.toCharArray()){
    	        if(hash[c2] > 0){
    	        	hash[c2]--;
    	            count++;
    	          }
    	    }
    	        
    	    if(count == s.length())
    	        return true;
    	    
    	    return false;
        }  
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.