Skip to content

Commit 2c3043a

Browse files
committed
regular expression
1 parent bb3f603 commit 2c3043a

25 files changed

+3028
-2517
lines changed

Java/Delete Digits.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
M
2-
Tags: Greedy, Priority Queue
2+
1534317486
3+
tags: Greedy, Priority Queue
34

45
#### Priority Queue
56
- TODO: parse into node(index, digitValue)

Java/Flatten 2D Vector.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
M
2+
1534317715
23
tags: Design
34

45
Implement an iterator to flatten a 2d vector.
56

7+
Just move pointers carefully with next(), hashNext()
8+
69
#### Basic Implementation using x, y corrdinate
710
- 就是把2D list里面的element全部遍历一遍
811
- 跟一个nxn的matrix遍历是没区别的拉; 所有来个x,y把2d list跑一变

Java/Longest Substring with At Most K Distinct Characters.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
M
1+
H
22
1520228394
3-
tags: Hash Table, String
3+
tags: Hash Table, String, Sliding Window
44

55
大清洗 O(nk)
66
map.size一旦>k要把longest string最开头marked by pointer:start的那个char抹掉
@@ -36,27 +36,23 @@ public int lengthOfLongestSubstringKDistinct(String s, int k) {
3636
return 0;
3737
}
3838
int n = s.length();
39-
int start = 0;
40-
int end = 0;
41-
int rst = Integer.MIN_VALUE;
39+
int start = 0, end = 0, rst = Integer.MIN_VALUE;
4240
HashMap<Character, Integer> map = new HashMap<>();
43-
while (start < n) {
41+
while (start < n) { // left window
4442
while (end < n) {
4543
char c = s.charAt(end);
4644
if (map.containsKey(c)) {
4745
map.put(c, map.get(c) + 1);
4846
} else {
49-
if (map.size() == k) {
50-
break;
51-
}
47+
if (map.size() == k) break; // meet window size
5248
map.put(c, 1);
5349
}
5450
end++;
5551
}
56-
// Calculate length when map.size() == n or end == n
52+
// Calculate length when map.size() == k or end == n
5753
rst = Math.max(rst, end - start);
5854

59-
// move start forward
55+
// move start forward and clean up map
6056
char c = s.charAt(start);
6157
int count = map.get(c);
6258
if (count == 1) {
@@ -71,7 +67,6 @@ public int lengthOfLongestSubstringKDistinct(String s, int k) {
7167
}
7268
}
7369

74-
7570
/*
7671
Thoughts:
7772
HashMap size cannot go over k.

Java/Regular Expression Matching.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
R
2-
1519287323
3-
tags: String, DP, Backtracking
1+
H
2+
1534348524
3+
tags: String, DP, Sequence DP, Double Sequence DP, Backtracking
4+
5+
跟WildCard Matching 一样, 分清楚情况讨论 string p last char is '*' 还有并不是 '*'
6+
7+
这里的区别是, '*' 需要有一个preceding element, 那么:
8+
- repeat 0 times
9+
- repeat 1 times: need s[i-1] match with prior char p[i-2]
410

511
```
612
/*
@@ -42,14 +48,8 @@ bool isMatch(const char *s, const char *p)
4248
*/
4349
class Solution {
4450
public boolean isMatch(String s, String p) {
45-
if (s == null || p == null) {
46-
return false;
47-
}
48-
if (s.length() != p.length() && p.indexOf('.') < 0 && p.indexOf('*') < 0) {
49-
return false;
50-
}
51-
int m = s.length();
52-
int n = p.length();
51+
if (s == null || p == null) return false;
52+
int m = s.length(), n = p.length();
5353
boolean[][] dp = new boolean[m + 1][n + 1];
5454
char[] ss = s.toCharArray();
5555
char[] pp = p.toCharArray();
@@ -60,7 +60,7 @@ public boolean isMatch(String s, String p) {
6060
dp[i][j] = true;
6161
continue;
6262
}
63-
if (i >= 1 && j == 0) {
63+
if (j == 0) { // When p is empty but s is not empty, should not match
6464
dp[i][j] = false;
6565
continue;
6666
}
@@ -72,10 +72,11 @@ public boolean isMatch(String s, String p) {
7272
dp[i][j] = dp[i - 1][j - 1];
7373
}
7474
} else { // tail = '*'. ex: a*
75-
if (j >= 2 ) { // ignore a*
76-
dp[i][j] = dp[i][j - 2];
75+
if (j >= 2 ) { // ignore a*, repeat 0 times
76+
dp[i][j] |= dp[i][j - 2];
7777
}
78-
if (j >= 2 && i >= 1 && (pp[j - 2] == '.' || pp[j - 2] == ss[i - 1])) {
78+
// repeat the char befeore * for 1 time, so ss[i-1] should match pp[j-2] or pp[j-2] == '.'
79+
if (j >= 2 && i >= 1 && (ss[i - 1] == pp[j - 2] || pp[j - 2] == '.')) {
7980
dp[i][j] |= dp[i - 1][j];
8081
}
8182
}

Java/The Spiral Matrix II.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
M
2+
1534346957
3+
tags: Array
4+
5+
#### Move forward till end
6+
- Similar concept as `The Maze`: keep walking until hit wall, turn back
7+
- fix direction `dx[direction % 4]`
8+
9+
```
10+
/*
11+
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
12+
13+
Example:
14+
15+
Input: 3
16+
Output:
17+
[
18+
[ 1, 2, 3 ],
19+
[ 8, 9, 4 ],
20+
[ 7, 6, 5 ]
21+
]
22+
*/
23+
24+
/*
25+
Use while loop, reach end and stop
26+
*/
27+
class Solution {
28+
int[] dx = {0, 1, 0, -1};
29+
int[] dy = {1, 0, -1, 0};
30+
public int[][] generateMatrix(int n) {
31+
int[][] grid = new int[n][n];
32+
int step = 1, i = 0, j = 0, direction = 0;
33+
grid[i][j] = step++;
34+
while (step <= n * n) {
35+
int x = dx[direction % 4];
36+
int y = dy[direction % 4];
37+
i += x;
38+
j += y;
39+
while (i >= 0 && i < n && j >= 0 && j < n && grid[i][j] == 0) {
40+
grid[i][j] = step++;
41+
i += x;
42+
j += y;
43+
}
44+
i -= x;
45+
j -= y;
46+
direction++;
47+
}
48+
return grid;
49+
}
50+
}
51+
```

Java/Wildcard Matching.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
H
2-
1519368767
3-
tags: String, DP, Backtracking, Greedy
2+
1534348528
3+
tags: String, DP, Sequence DP, Double Sequence DP, Backtracking, Greedy
44

55
Double sequence DP. 与regular expression 很像.
66

7-
注意1: 分析字符 ?, * 所代表的真正意义, 然后写出表达式.
8-
注意2: 搞清楚initialization 的时候 dp[i][0] 应该always false.当p为empty string, 无论如何都match不了 (除非s="" as well)
9-
同时 dp[0][j]不一定是false. 比如s="",p="*" 就是一个matching.
7+
#### Double Sequence DP
8+
- 分析字符 ?, * 所代表的真正意义, 然后写出表达式.
9+
- 搞清楚initialization 的时候 dp[i][0] 应该always false. 当p为empty string, 无论如何都match不了 (除非s="" as well)
10+
- 同时 dp[0][j]不一定是false. 比如s="",p="*" 就是一个matching.
11+
- A. p[j] != '*'
12+
1. last index match => dp[i - 1][j - 1]
13+
2. last index == ? => dp[i - 1][j - 1]
14+
- B. p[j] == "*"
15+
1. * is empty => dp[i][j - 1]
16+
2. * match 1 or more chars => dp[i - 1][j]
17+
1018

1119
```
1220
/*
@@ -51,17 +59,12 @@ Time,Space O(MN)
5159
*/
5260
class Solution {
5361
public boolean isMatch(String s, String p) {
54-
if (s == null || p == null) {
55-
return false;
56-
}
57-
int m = s.length();
58-
int n = p.length();
62+
if (s == null || p == null) return false;
63+
int m = s.length(), n = p.length();
5964
boolean[][] dp = new boolean[m + 1][n + 1];
6065
char[] ss = s.toCharArray();
6166
char[] pp = p.toCharArray();
6267

63-
// dp[0][j] = false; dp[i][0] = false;
64-
6568
for (int i = 0; i <= m; i++) {
6669
for (int j = 0; j <= n; j++) {
6770
if (i == 0 && j == 0) {
@@ -75,7 +78,7 @@ public boolean isMatch(String s, String p) {
7578
dp[i][j] = false;
7679
if (pp[j - 1] != '*') {
7780
if (i >= 1 && (ss[i - 1] == pp[j - 1] || pp[j - 1] == '?')) {
78-
dp[i][j] = dp[i - 1][j - 1];
81+
dp[i][j] |= dp[i - 1][j - 1];
7982
}
8083
} else {
8184
dp[i][j] |= dp[i][j - 1];// '*' -> empty

0 commit comments

Comments
 (0)