File tree Expand file tree Collapse file tree 3 files changed +118
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure Expand file tree Collapse file tree 3 files changed +118
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ Time Complexity : O(n)
3+ Space Complexity : O(1)
4+ */
5+ class Solution {
6+ public int maxArea (int [] height ) {
7+ int left = 0 ;
8+ int right = height .length - 1 ;
9+ int area = 0 ;
10+
11+ while (left < right ) {
12+ int currentArea = (right - left ) * Math .min (height [left ], height [right ]);
13+ area = Math .max (area , currentArea );
14+ if (height [left ] < height [right ]) {
15+ left ++;
16+ } else {
17+ right --;
18+ }
19+ }
20+ return area ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ /*
2+ m : word length
3+ n : Trie node count
4+ addWord
5+ Time Complexity: O(m)
6+
7+ search
8+ Time Complexity: O(n)
9+
10+ Space Complexity: O(26 × N × M)
11+
12+ Trie + Dfs
13+ */
14+
15+ class WordDictionary {
16+ class TrieNode {
17+ TrieNode [] children ;
18+ boolean isEnd ;
19+
20+ public TrieNode () {
21+ children = new TrieNode [26 ];
22+ isEnd = false ;
23+ }
24+ }
25+
26+ private TrieNode root ;
27+
28+ public WordDictionary () {
29+ root = new TrieNode ();
30+ }
31+
32+ public void addWord (String word ) {
33+ TrieNode current = root ;
34+
35+ for (char str : word .toCharArray ()) {
36+ int idx = str - 'a' ;
37+ if (current .children [idx ] == null ) {
38+ current .children [idx ] = new TrieNode ();
39+ }
40+ current = current .children [idx ];
41+ }
42+ current .isEnd = true ;
43+ }
44+
45+ public boolean search (String word ) {
46+ return dfsSearch (word , 0 , root );
47+ }
48+
49+ private boolean dfsSearch (String word , int idx , TrieNode node ) {
50+ if (idx == word .length ()) {
51+ return node .isEnd ;
52+ }
53+
54+ char c = word .charAt (idx );
55+ if (c == '.' ) {
56+ for (int i = 0 ; i < 26 ; i ++) {
57+ if (node .children [i ] != null ) {
58+ if (dfsSearch (word , idx + 1 , node .children [i ])) {
59+ return true ;
60+ }
61+ }
62+ }
63+ return false ;
64+ } else {
65+ int charIdx = c - 'a' ;
66+ if (node .children [charIdx ] == null ) {
67+ return false ;
68+ }
69+ return dfsSearch (word , idx + 1 , node .children [charIdx ]);
70+ }
71+ }
72+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public boolean isValid (String s ) {
3+ if (s == null || s .length () % 2 != 0 ) {
4+ return false ;
5+ }
6+ Stack <Character > stack = new Stack <>();
7+ HashMap <Character , Character > strList = new HashMap <>();
8+ strList .put (')' , '(' );
9+ strList .put (']' , '[' );
10+ strList .put ('}' , '{' );
11+
12+ for (char c : s .toCharArray ()) {
13+ if (strList .containsKey (c )) {
14+ if (stack .isEmpty () || stack .pop () != strList .get (c )) {
15+ return false ;
16+ }
17+ } else {
18+ stack .push (c );
19+ }
20+ }
21+
22+ return stack .isEmpty ();
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments