-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordPattern.java
More file actions
29 lines (24 loc) · 914 Bytes
/
WordPattern.java
File metadata and controls
29 lines (24 loc) · 914 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
26
27
28
29
class WordPattern {
public boolean wordPattern(String pattern, String s) {
String[] words=s.split(" ");
if(pattern.length()!= words.length)
return false;
HashMap<Character,String> map=new HashMap<>();
HashMap <String,Boolean> used=new HashMap<>();
for(int i=0;i<pattern.length();i++){
char ch =pattern.charAt(i);
if(map.containsKey(ch)==false){
if(used.containsKey(words[i])==true)
return false;
else{
used.put(words[i],true);
map.put(ch,words[i]);
}}
else{
String matcch=map.get(ch);
if(matcch.equals(words[i])==false)
return false;
}
}
return true;}
}