We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1a0dd0d commit e9ca37aCopy full SHA for e9ca37a
solution/0290.Word Pattern/Solution.java
@@ -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
13
14
+ map1.put(c, i);
15
+ map2.put(split[i], i);
16
17
+ return true;
18
19
+}
0 commit comments