File tree 2 files changed +25
-0
lines changed
2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change
1
+ package easy ;
2
+
3
+ import java .util .HashMap ;
4
+ import java .util .Map ;
5
+
6
+ public class WordPattern {
7
+
8
+ public boolean wordPattern (String pattern , String str ) {
9
+ String [] words = str .split (" " );
10
+ char [] patterns = pattern .toCharArray ();
11
+ Map <Character , String > map = new HashMap ();
12
+ if (patterns .length != words .length ) return false ;
13
+ for (int i = 0 ; i < patterns .length ; i ++){
14
+ if (map .containsKey (patterns [i ])){
15
+ if (!map .get (patterns [i ]).equals (words [i ])) return false ;
16
+ } else {
17
+ if (map .containsValue (words [i ])) return false ;//this is for this case: "abba", "dog dog dog dog"
18
+ map .put (patterns [i ], words [i ]);
19
+ }
20
+ }
21
+ return true ;
22
+ }
23
+
24
+ }
Original file line number Diff line number Diff line change 23
23
|301|[ Remove Invalid Parentheses] ( https://leetcode.com/problems/remove-invalid-parentheses/ ) |[ Solution] | ? | ? | Hard| BFS
24
24
| 299| [ Bulls and Cows] ( https://leetcode.com/problems/bulls-and-cows/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/BullsandCows.java ) | O(n)| O(1) | Easy|
25
25
| 292| [ Nim Game] ( https://leetcode.com/problems/nim-game/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/NimGame.java ) | O(1)| O(1) | Easy|
26
+ | 290| [ Word Pattern] ( https://leetcode.com/problems/word-pattern/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/WordPattern.java ) | O(n)| O(n) | Easy|
26
27
| 289| [ Game of Life] ( https://leetcode.com/problems/game-of-life/ ) | [ Solution] ( ../../blob/master/MEDIUM/src/medium/GameOfLife.java ) | O(m* n)| O(m* n), could be optimized to O(1) | Medium|
27
28
| 283| [ Move Zeroes] ( https://leetcode.com/problems/move-zeroes/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/MoveZeroes.java ) | O(n)| O(1) | Easy|
28
29
| 280| [ Wiggle Sort] ( https://leetcode.com/problems/wiggle-sort/ ) | [ Solution] ( ../../blob/master/MEDIUM/src/medium/WiggleSort.java ) | O(n)| O(1) | Medium|
You can’t perform that action at this time.
0 commit comments