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
// wordBreak("facebook", ["face", "book"]) -> true | |
public boolean wordBreak(String s, List<String> wordDict) { | |
Set<String> set = new HashSet<>(wordDict); | |
if(set.contains(s)) | |
return true; | |
Queue<Integer> queue = new LinkedList<>(); | |
Set<Integer> visited = new HashSet<>(); | |
queue.add(0); | |
visited.add(0); |
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<String> binaryTreePaths(TreeNode root) { | |
List<String> list = new ArrayList<>(); | |
if(root != null)getPaths(root, list, ""); | |
return list; | |
} | |
private static void getPaths(TreeNode root, List<String> list, String path) { | |
if(root.left == null && root.right == null) list.add(path + root.val); | |
if(root.left != null) getPaths(root.left, list, path + root.val + "->"); | |
if(root.right != null) getPaths(root.right, list, path + root.val + "->"); |
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 static String getClean(String pattern){ | |
StringBuilder sb = new StringBuilder(); | |
sb.append(pattern.charAt(0)); | |
for(int i = 1; i < pattern.length(); i++) { | |
char ch = pattern.charAt(i); | |
if(ch != '*') sb.append(ch); | |
else { | |
char lastChar = sb.charAt(sb.length()-1); | |
if(lastChar!='*') sb.append(ch); |
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 static int ladderLength(String beginWord, String endWord, List<String> wordList) { | |
int count = 1; | |
if(beginWord.equals(endWord)) | |
return count; | |
Queue<String> queue = new LinkedList<>(); | |
Set<String> set = new HashSet<String>(wordList); // contains() is O(1). For ArrayList it's O(n) | |
queue.add(beginWord); | |
set.remove(beginWord); |
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
Map<Integer, UndirectedGraphNode> map = new HashMap<>(); | |
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { | |
if(node == null) return null; | |
if(map.containsKey(node.label)) | |
return map.get(node.label); | |
UndirectedGraphNode clonedNode = new UndirectedGraphNode(node.label); | |
map.put(clonedNode.label, clonedNode); | |
for(UndirectedGraphNode neighbor : node.neighbors) | |
clonedNode.neighbors.add(cloneGraph(neighbor)); |
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
import java.util.SortedMap; | |
class CriticalPoint implements Comparable<CriticalPoint>{ | |
int x; | |
int y; | |
int isRightEnd; | |
CriticalPoint(int x, int y, int isRightEnd) { | |
this.x = x; | |
this.y = y; | |
this.isRightEnd = isRightEnd; |
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[2][3] | |
1 2 3 | |
4 5 6 | |
b[3][2] | |
7 8 | |
9 10 | |
11 12 |
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
// dp[0] = 0 | |
// dp[i] = min(dp[n-i*i] + 1) where i > 0 && n-i*i >=0 | |
public int numSquares(int n) { | |
int dp[] = new int[n+1]; | |
dp[0] = 0; | |
for(int i = 1; i <= n; i++) { | |
int min = Integer.MAX_VALUE; | |
int j = 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 String reverseWords(String s) { | |
String[] a = s.replaceAll("\\s+", " ").split(" "); | |
int i = 0, j = a.length-1; | |
while(i < j) { | |
String temp = a[i]; | |
a[i++] = a[j]; | |
a[j--] = temp; | |
} | |
return String.join(" ", a).trim(); |
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<String> removeInvalidParentheses(String s) { | |
List<String> list = new ArrayList<>(); | |
backtrack(list, s, 0, 0, new char[]{'(', ')'}); | |
return list; | |
} | |
private static void backtrack(List<String> list, String s, int iStart, int jStart, char[] par) { | |
int stack = 0; | |
for(int i = iStart; i < s.length(); i++) { | |
if(s.charAt(i) == par[0]) stack++; |