-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordPattern.java
More file actions
148 lines (104 loc) · 3.82 KB
/
WordPattern.java
File metadata and controls
148 lines (104 loc) · 3.82 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package Algorithms.Hashing;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
*
*
* Here one letter from pattern word is mapped to one word from s sentence
* Pattern = "abba"
* s = "dog cat cat dog"
* here a is mapped to dog and b is mapped to cat
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 22 Sept 2024
* @link 290. Word Pattern <a href="https://leetcode.com/problems/word-pattern/">Leetcode link</a>
* @topics Hash Table, String
* @see Algorithms.Hashing.IsomorphicStrings
Examples for easy understanding
s = "paper"
t = "title"
TRUE
here 'e' is present in both s and t
s = "badc"
t = "baba"
FALSE
here b-b, a-a, d-b, c-a ---> 'b' value and 'a' value are repeated in HashMap values multiple times
but as per isomorphic, the values must be unique too
*/
public class WordPattern {
public static void main(String[] args) {
String pattern = "abba"; // abba
String s = "dog dog dog dog"; // dog cat cat dog"
sout("wordPattern 1 => " + wordPattern(pattern, s));
sout("wordPattern 2 => " + wordPattern2(pattern, s));
sout("wordPattern 3 => " + wordPattern3(pattern, s));
sout("wordPattern 4 => " + wordPattern4(pattern, s));
}
public static boolean wordPattern(String pattern, String s) {
String[] strs = s.split(" ");
if(pattern.length() != strs.length) {
return false;
}
Map<Character, String> pToS = new HashMap<>();
for(int i=0; i<pattern.length(); i++) {
char c = pattern.charAt(i);
if(pToS.containsKey(c) && !pToS.get(c).equals(strs[i])) {
return false;
}
pToS.put(c, strs[i]);
}
return pToS.size() == new HashSet<>(pToS.values()).size();
}
public static boolean wordPattern2(String pattern, String s) {
String[] strs = s.split(" ");
if(pattern.length() != strs.length) {
return false;
}
Map<Character, String> pToS = new HashMap<>();
Map<String, Character> sToP = new HashMap<>();
for(int i=0; i<pattern.length(); i++) {
char c = pattern.charAt(i);
if(pToS.containsKey(c) && !pToS.get(c).equals(strs[i])) return false;
if(sToP.containsKey(strs[i]) && !sToP.get(strs[i]).equals(c)) return false;
pToS.put(c, strs[i]);
sToP.put(strs[i], c);
}
return true;
}
public static boolean wordPattern3(String pattern, String s) {
String[] strs = s.split(" ");
if(pattern.length() != strs.length) {
return false;
}
Map<Character, String> pToS = new HashMap<>();
Set<String> seen = new HashSet<>();
for(int i=0; i<pattern.length(); i++) {
char c = pattern.charAt(i);
if(pToS.containsKey(c) && !pToS.get(c).equals(strs[i])) return false;
if(!pToS.containsKey(c) && seen.contains(strs[i])) return false;
pToS.put(c, strs[i]);
seen.add(strs[i]);
}
return true;
}
public static boolean wordPattern4(String pattern, String s) {
String[] letters = pattern.split("");
String[] words = s.split(" ");
if (letters.length != words.length) return false;
Map<String, String> map = new HashMap<>();
for(int i = 0; i< letters.length; i++){
var key = letters[i];
var val = words[i];
if(!map.containsKey(key)) {
if (map.containsValue(val)) return false;
map.put(key, val);
}
else{
if (!map.get(key).equals(val)) return false;
}
}
return true;
}
public static <E> void sout(E s){ System.out.println(s);}
}