1. 程式人生 > >290. Word Pattern#2(Done)

290. Word Pattern#2(Done)

Solution

public class Solution {
    public boolean wordPattern(String pattern, String str) {
        HashMap<Character, String> map = new HashMap<Character, String>();
        char[] cArray = pattern.toCharArray();
        String[] sArray = str.split(" ");
        if (cArray.length != sArray.length) {
            return
false; } for (int i = 0; i < cArray.length; i++) { if (map.contains(cArray[i])) { if (map.get(cArray[i]) != sArray[i]) { return false; } } else { map.put(cArray[i], sArray[i]); } } return
true; } }

Problem#2

  • map的put方法
  • map中不能存int

Problem#1

  • map的方法不熟悉, put(), containsKey()
  • 解法二,存位置到字典,