Skip to content

Commit e9ca37a

Browse files
authored
Create Solution.java
1 parent 1a0dd0d commit e9ca37a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public boolean wordPattern(String pattern, String str) {
3+
String[] split = str.split(" ");
4+
if (pattern.length() != split.length) {
5+
return false;
6+
}
7+
Map<Character, Integer> map1 = new HashMap<>();
8+
Map<String, Integer> map2 = new HashMap<>();
9+
for (int i = 0; i < pattern.length(); ++i) {
10+
char c = pattern.charAt(i);
11+
if (!map1.getOrDefault(c, -1).equals(map2.getOrDefault(split[i], -1))) {
12+
return false;
13+
}
14+
map1.put(c, i);
15+
map2.put(split[i], i);
16+
}
17+
return true;
18+
}
19+
}

0 commit comments

Comments
 (0)