MCPcopy Create free account
hub / github.com/MisterBooo/LeetCodeAnimation / Solution

Class Solution

problems/0290-Word-Pattern/Code/1.java:1–26  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected