【leetcode】290 (Easy) Word Pattern

提交代码

class Solution {
    public boolean wordPattern(String pattern, String str) {
    	char[] chars = pattern.toCharArray();
    	String[] words = str.split(" ");
    	if(chars.length!=words.length)	return false;
    	int len = chars.length;
    	
    	Map<Character, String> mapCharWord = new HashMap<Character, String>();
    	Map<String, Character> mapWordChar = new HashMap<String, Character>();
    	
    	for(int i=0;i<len;i++) {
    		if(!mapCharWord.containsKey(chars[i])) 
    			mapCharWord.put(chars[i], words[i]);
    		else
    			if(!words[i].equals(mapCharWord.get(chars[i])))
    				return false;
    	}
    	
    	for(int i=0;i<len;i++) {
    		if(!mapWordChar.containsKey(words[i])) 
    			mapWordChar.put(words[i], chars[i]);
    		else
    			if(chars[i]!=mapWordChar.get(words[i]))
    				return false;
    	}
    	
    	return true;
    }
}

运行结果

【leetcode】290 (Easy) Word Pattern_第1张图片

你可能感兴趣的:(leetcode)