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.
2 parents ede37bd + 4a621f6 commit 4300f05Copy full SHA for 4300f05
wordPattern.java
@@ -0,0 +1,30 @@
1
+class Solution {
2
+ public boolean wordPattern(String pattern, String s) {
3
+ String[] words = s.split(" ");
4
+ if (words.length != pattern.length()) {
5
+ return false;
6
+ }
7
+
8
+ Map<Character, String> charToWord = new HashMap<>();
9
+ Map<String, Character> wordToChar = new HashMap<>();
10
11
+ for (int i = 0; i < pattern.length(); i++) {
12
+ char c = pattern.charAt(i);
13
+ String word = words[i];
14
15
+ if (!charToWord.containsKey(c)) {
16
+ charToWord.put(c, word);
17
18
19
+ if (!wordToChar.containsKey(word)) {
20
+ wordToChar.put(word, c);
21
22
23
+ if (!charToWord.get(c).equals(word) || !wordToChar.get(word).equals(c)) {
24
25
26
27
28
+ return true;
29
30
+}
0 commit comments