290. Word Pattern#2(Done)
阿新 • • 發佈:2018-12-30
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()
- 解法二,存位置到字典,