# Leetcode 290. Word Pattern ###### tags: `Leetcode(JAVA)` 題目 : https://leetcode.com/problems/word-pattern/ 。 想法 : 用MAP映射字元到對應的字串,如果不同就返回False,否則為True。 時間複雜度 : O(n)。 程式碼 : (JAVA) ``` class Solution { public boolean wordPattern(String pattern, String s) { String[] arr=s.split(" "); if(pattern.length() != arr.length) return false; Map<Character, String> map=new HashMap<>(); Map<String, Character> rev=new HashMap<>(); for(int i=0 ; i<arr.length ; i++){ char p=pattern.charAt(i); String sub=arr[i]; if(map.get(p) == null && rev.get(sub) == null){ map.put(p, sub); rev.put(sub, p); } else if(map.get(p) == null || rev.get(sub) == null){ return false; } else{ if(map.get(p).equals(sub) && rev.get(sub).equals(p)){ continue; } else{ return false; } } } return true; } } ```