leetcode算法之Valid Anagram

原文算法说明如下:

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.

翻译:判断给定的两个字符串是否为打乱了顺序的同一个字符串

我的java实现:

import java.util.HashMap;
public class Solution {
    public boolean isAnagram(String s, String t) {
    	if(s.length() != t.length()){
    		return false;
    	}
    	char[] c1 = s.toCharArray();
    	char[] c2 = t.toCharArray();
    	HashMap<Character,Integer> map1 = new HashMap<Character,Integer>();
    	HashMap<Character,Integer> map2 = new HashMap<Character,Integer>();
    	for(int i=0;i < s.length();i++){
    		if(!map1.containsKey(c1[i])){
    			map1.put(c1[i], 1);
    		}else{
    			map1.put(c1[i], map1.get(c1[i])+1);
    		}
    		if(!map2.containsKey(c2[i])){
    			map2.put(c2[i], 1);
    		}else{
    			map2.put(c2[i], map2.get(c2[i])+1);
    		}
    	}
    	if(map1.entrySet().containsAll(map2.entrySet()) && map2.entrySet().containsAll(map1.entrySet())){
    		return true;
    	}
        return false;
    }
}



你可能感兴趣的:(leetcode算法之Valid Anagram)