| 1 | class Solution { |
| 2 | public boolean wordPattern(String pattern, String str) { |
| 3 | HashMap<Character, String> map = new HashMap<>(); |
| 4 | HashSet<String> set = new HashSet<>(); |
| 5 | String[] array = str.split(" "); |
| 6 | |
| 7 | if (pattern.length() != array.length) { |
| 8 | return false; |
| 9 | } |
| 10 | for (int i = 0; i < pattern.length(); i++) { |
| 11 | char key = pattern.charAt(i); |
| 12 | if (!map.containsKey(key)) { |
| 13 | if (set.contains(array[i])) { |
| 14 | return false; |
| 15 | } |
| 16 | map.put(key, array[i]); |
| 17 | set.add(array[i]); |
| 18 | } else { |
| 19 | if (!map.get(key).equals(array[i])) { |
| 20 | return false; |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | return true; |
| 25 | } |
| 26 | } |
nothing calls this directly
no outgoing calls
no test coverage detected