public boolean wordPattern(String pattern, String str) {
        if (pattern == null || str == null) return false;
        String[] words = str.split(" ");
        if (pattern.length() != words.length) return false;
        HashMap<Character, String> map1 = new HashMap<Character, String>();
        HashMap<String, Character> map2 = new HashMap<String, Character>();
        for (int i = 0; i < pattern.length(); ++i) {
            char ch = pattern.charAt(i);
            String word = words[i];
            if (map1.containsKey(ch) && !map1.get(ch).equals(word)) return false;
            if (map2.containsKey(word) && !map2.get(word).equals(ch)) return false;
            map1.put(ch, word);
            map2.put(word, ch);
        }
        return true;
    }