forked from fineanmol/Hacktoberfest2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
25 lines (19 loc) · 700 Bytes
/
Solution.java
File metadata and controls
25 lines (19 loc) · 700 Bytes
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
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] arr= s.split(" ");
HashMap<Character, String> map = new HashMap<>();
if(arr.length!= pattern.length())
return false;
for(int i = 0; i<arr.length; i++) {
if(map.containsKey(pattern.charAt(i))) {
if(!map.get(pattern.charAt(i)).equals(arr[i]))
return false;
}
else
if(map.containsValue(arr[i]))
return false;
map.put(pattern.charAt(i), arr[i]);
}
return true;
}
}