This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Below is the online solution that keeps a running sum, no extra space is needed other than recursion stack | |
private int accumulatingSum = 0; | |
public TreeNode convertBST(TreeNode root) { | |
// Reset the accumulating sum | |
this.accumulatingSum = 0; | |
this.reverseInorderTraversal(root); | |
return root; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Stack<Integer> stack = new Stack<>(); | |
private Stack<Integer> minStack = new Stack<>(); | |
public void push(int x) { | |
stack.add(x); | |
if (minStack.isEmpty()) { | |
minStack.add(x); | |
} else { | |
// Here I am only adding the element if it is <= min, another way is always add minium |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MinStackNode { | |
public int val; | |
public int curMin; | |
public MinStackNode(int val, int curMin) { | |
this.val = val; | |
this.curMin = curMin; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<List<String>> groupAnagrams(String[] strs) { | |
List<List<String>> res = new ArrayList<>(); | |
if (strs == null || strs.length == 0) { | |
return res; | |
} | |
Map<String, List<String>> lookup = new HashMap<>(); | |
for (String s : strs) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
// good practice to define constant | |
public static final int MAX = 256; | |
public List<Integer> findAnagrams(String s, String p) { | |
List<Integer> res = new ArrayList<>(); | |
if (p == null || s == null || s.length() == 0 || p.length() > s.length()) { | |
return res; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A quick implementation, a bit duplicate on checking on the palindrome part | |
public boolean validPalindrome(String s) { | |
// It says s is non-empty, the other is covered by normal case, so don't need this check | |
if (s == null || s.length() <= 2) { | |
return true; | |
} | |
int i = 0; | |
int j = s.length() - 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Need an intermediate Result class to return multiple values from a function | |
public class Result{ | |
public boolean isPalindrome = false; | |
public int diffStartIndex = 0; | |
public int diffEndIndex = 0; | |
public Result(boolean isPalindrome, int diffStartIndex, int diffEndIndex) { | |
this.isPalindrome = isPalindrome; | |
this.diffStartIndex = diffStartIndex; | |
this.diffEndIndex = diffEndIndex; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean isMatchTLE(String s, String p) { | |
if (s == null || p == null) { | |
return false; | |
} | |
return isMatchHelper(s, 0, p, 0); | |
} | |
// b*b*ab**ba*b**b***bba , this should trim some of the recursion time | |
public String removeDuplicateStars(String p) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the DP solution, and falls perfectly using the DP template | |
// Also this is only boolean answer (not find all steps) || find the extreme values (min,max) | |
public boolean isMatch(String s, String p) { | |
if (s == null || p == null) { | |
return false; | |
} | |
int row = s.length(); | |
int col = p.length(); | |
boolean[][] lookup = new boolean[row + 1][col + 1]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean isMatch(String s, String p) { | |
if (s == null || p == null) { | |
return false; | |
} | |
int row = s.length(); | |
int col = p.length(); | |
boolean[][] lookup = new boolean[row + 1][col + 1]; |