File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 88
88
| 276 | [ Paint Fence] ( https://leetcode.com/problems/paint-fence ) | Easy | |
89
89
| 278 | [ First Bad Version] ( https://leetcode.com/problems/first-bad-version ) | Easy | [ ![ Java] ( https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png )] ( [] ) |
90
90
| 283 | [ Move Zeroes] ( https://leetcode.com/problems/move-zeroes ) | Easy | [ ![ Java] ( https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png )] ( https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/MoveZeros.java ) |
91
- | 290 | [ Word Pattern] ( https://leetcode.com/problems/word-pattern ) | Easy | |
91
+ | 290 | [ Word Pattern] ( https://leetcode.com/problems/word-pattern ) | Easy | [ ![ Java ] ( https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png )] ( ) |
92
92
| 292 | [ Nim Game] ( https://leetcode.com/problems/nim-game ) | Easy | |
93
93
| 293 | [ Flip Game] ( https://leetcode.com/problems/flip-game ) | Easy | |
94
94
| 299 | [ Bulls and Cows] ( https://leetcode.com/problems/bulls-and-cows ) | Easy | |
Original file line number Diff line number Diff line change
1
+ import java .util .HashMap ;
2
+ import java .util .HashSet ;
3
+ import java .util .Map ;
4
+ import java .util .Set ;
5
+
6
+ public class WordPattern {
7
+ public static boolean wordPattern (String pattern , String string ) {
8
+ Map <Character , String > bijection = new HashMap <>();
9
+ Set <String > wordsMappedTo = new HashSet <>();
10
+ String [] words = string .split (" " );
11
+ if (words .length != pattern .length ()) {
12
+ return false ;
13
+ }
14
+
15
+ for (int index = 0 ; index < pattern .length () ; index ++) {
16
+ char letter = pattern .charAt (index );
17
+ String word = words [index ];
18
+
19
+ if (!bijection .getOrDefault (letter , word ).equals (word ) ||
20
+ (!bijection .containsKey (letter ) && wordsMappedTo .contains (word ))) {
21
+ return false ;
22
+ }
23
+
24
+ bijection .put (letter , word );
25
+ wordsMappedTo .add (word );
26
+ }
27
+
28
+ return true ;
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments