File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments