Two Solutions in Java - Sort (348ms) & HashMap (424ms)


  • 1
    H
    public boolean isAnagramSort(String s, String t) {
    		if (s == null || t == null) {
    			return (s == null && t == null) ? true : false;
    		}
    		if (s.equals(t)) {
    			return true;
    		}
    		if (s.length() != t.length()) {
    			return false;
    		}
    		
    		char[] s1 = s.toCharArray();
    		char[] t1 = t.toCharArray();
    		
    		Arrays.sort(s.toCharArray());
    		Arrays.sort(t.toCharArray());
    		
    		return  new String(s1).equals(new String(t1)) ? true : false;
        }
    	
    	public boolean isAnagramHashMap(String s, String t) {
    		if (s == null || t == null) {
    			return (s == null && t == null) ? true : false;
    		}
    		if (s.equals(t)) {
    			return true;
    		}
    		if (s.length() != t.length()) {
    			return false;
    		}
    		
    		HashMap<Character, Integer> hash = new HashMap<Character, Integer>();
    		for (int index = 0; index < s.length(); index ++) {
    			if (hash.containsKey(s.charAt(index))) {
    				hash.put(s.charAt(index), hash.get(s.charAt(index)) + 1);
    			} else {
    				hash.put(s.charAt(index), 1);
    			}
    		}
    		for (int index = 0; index < t.length(); index ++) {
    			if (hash.containsKey(t.charAt(index))) {
    				hash.put(t.charAt(index), hash.get(t.charAt(index)) - 1);
    			} else {
    				return false;
    			}
    		}
    		
    		Iterator<Integer> it = hash.values().iterator();
    		while (it.hasNext()) {
    			if (it.next() != 0) {
    				return false;
    			}
    		}
    		return true;
        }

Log in to reply
 

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