File tree Expand file tree Collapse file tree 4 files changed +92
-0
lines changed
longest-palindromic-substring
validate-binary-search-tree Expand file tree Collapse file tree 4 files changed +92
-0
lines changed Original file line number Diff line number Diff line change 1+ // TC: O(n^2)
2+ // for loop & nested while loop => total O(n^2)
3+ // SC: O(1)
4+ // uses only constant space => O(1)
5+ class Solution {
6+ public String longestPalindrome (String s ) {
7+ int right = 0 ;
8+ int left = 0 ;
9+
10+ for (int i = 0 ; i < s .length (); i ++) {
11+ int even = pad (s , i , i );
12+ int odd = pad (s , i , i +1 );
13+ int max = Math .max (even , odd );
14+
15+ if (max > (right - left )) {
16+ right = i + (max + 1 ) / 2 ;
17+ left = i - max / 2 ;
18+ }
19+ }
20+
21+ return s .substring (left , right +1 );
22+ }
23+
24+ private int pad (String s , int start , int end ) {
25+ while (start >= 0 && end < s .length () && s .charAt (start ) == s .charAt (end )) {
26+ start -= 1 ;
27+ end += 1 ;
28+ }
29+ return start - end ;
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ // TC: O(n^2)
2+ // visit all elements with nested for-loop
3+ // SC: O(1)
4+ // constant space complexity
5+ class Solution {
6+ public void rotate (int [][] matrix ) {
7+ int n = matrix .length ;
8+
9+ for (int i = 0 ; i < (n + 1 ) / 2 ; i ++) {
10+ for (int j = 0 ; j < n / 2 ; j ++) {
11+ int temp = matrix [n -1 -j ][i ];
12+ matrix [n -1 -j ][i ] = matrix [n -1 -i ][n -1 -j ];
13+ matrix [n -1 -i ][n -1 -j ] = matrix [j ][n -1 -i ];
14+ matrix [j ][n -1 -i ] = matrix [i ][j ];
15+ matrix [i ][j ] = temp ;
16+ }
17+ }
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ // TC: O(n * m)
2+ // need to check all nodes and subRoot nodes in the worst case
3+ // SC: O(h1 + h2)
4+ // the high of root = h1 + the high of subRoot h2 => O(h1 + h2)
5+ class Solution {
6+ public boolean isSubtree (TreeNode root , TreeNode subRoot ) {
7+ if (root == null ) return false ;
8+
9+ if (sameCheck (root , subRoot )) return true ;
10+
11+ return isSubtree (root .left , subRoot ) ||
12+ isSubtree (root .right , subRoot );
13+ }
14+
15+ private boolean sameCheck (TreeNode root , TreeNode subRoot ) {
16+ if (root == null || subRoot == null ) {
17+ return root == null && subRoot == null ;
18+ }
19+
20+ if (root .val != subRoot .val ) return false ;
21+
22+ return sameCheck (root .left , subRoot .left ) &&
23+ sameCheck (root .right , subRoot .right );
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ // TC: O(n)
2+ // visit all nodes to compare
3+ // SC: O(n)
4+ // recursion requires O(n) SC in the worst case
5+ class Solution {
6+ public boolean isValidBST (TreeNode root ) {
7+ return dfs (root , Long .MIN_VALUE , Long .MAX_VALUE );
8+ }
9+
10+ private boolean dfs (TreeNode node , long min , long max ) {
11+ if (node == null ) return true ;
12+
13+ if (node .val <= min || node .val >= max ) return false ;
14+ return dfs (node .left , min , node .val ) &&
15+ dfs (node .right , node .val , max );
16+ }
17+ }
You can’t perform that action at this time.
0 commit comments