Skip to content

Commit f56d1ba

Browse files
Merge pull request neetcode-gh#2 from tharunkanna14/tharunkanna14-patch-2
Create 290-word-pattern
2 parents 914a2e4 + 157ab60 commit f56d1ba

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

java/290-word-pattern

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public boolean wordPattern(String pattern, String s) {
3+
String[] sArray = s.split("\s");
4+
if(sArray.length != pattern.length()) {
5+
return false;
6+
}
7+
8+
HashMap<Character,String> charToWord = new HashMap<>();
9+
HashMap<String,Character> wordToChar = new HashMap<>();
10+
11+
for (int i = 0; i < pattern.length(); i++) {
12+
13+
if(charToWord.containsKey(pattern.charAt(i)) && !charToWord.get(pattern.charAt(i)).equals(sArray[i])) {
14+
return false;
15+
}
16+
17+
if(wordToChar.containsKey(sArray[i]) && !wordToChar.get(sArray[i]).equals(pattern.charAt(i))) {
18+
return false;
19+
}
20+
21+
charToWord.put(pattern.charAt(i),sArray[i]);
22+
wordToChar.put(sArray[i],pattern.charAt(i));
23+
}
24+
return true;
25+
}
26+
}

0 commit comments

Comments
 (0)