-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordPattern.java
36 lines (31 loc) · 1.22 KB
/
wordPattern.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] words = s.split(" ");
if (pattern.length() != words.length) {
return false; // Length mismatch
}
Map<Character, String> charToWord = new HashMap<>();
Map<String, Character> wordToChar = new HashMap<>();
for (int i = 0; i < pattern.length(); i++) {
char currentChar = pattern.charAt(i);
String currentWord = words[i];
// Check if the character is already mapped to a word
if (charToWord.containsKey(currentChar)) {
if (!charToWord.get(currentChar).equals(currentWord)) {
return false; // Mismatch in mapping
}
} else {
charToWord.put(currentChar, currentWord);
}
// Check if the word is already mapped to a character
if (wordToChar.containsKey(currentWord)) {
if (!wordToChar.get(currentWord).equals(currentChar)) {
return false; // Mismatch in mapping
}
} else {
wordToChar.put(currentWord, currentChar);
}
}
return true;
}
}