Skip to content

Commit 16765a3

Browse files
committed
commit changes to gh-pages aug 25
1 parent 25e3a3f commit 16765a3

File tree

4 files changed

+141
-6
lines changed

4 files changed

+141
-6
lines changed
Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,79 @@
1+
## What is window substring like
2+
3+
```
4+
S = "ADOBECODEBANC"
5+
T = "ABC"
6+
```
7+
8+
Minimum window is "BANC". A string with 1 `A`, 1 `B` and 1 `C`.
9+
10+
Something you shuold notice:
11+
12+
* if a substring is a window string, it plus any string is also a window string.
13+
14+
`ADOBEC` is a window string, so `ADOBECODE` is a window string too.
15+
16+
* if a window string's left most or right most character, lets say `c`, is in `T` and the window string has more character `c` than `T` has, removing the `c` from the window string will not break that the remaining string is a window string.
17+
18+
`ADOBECODEBA` has 2 `A`, and more than `T` has (1 `A`). so, after remove `A` from left or right, the string remains a window string (`ADOBECODEB` is a window string).
19+
20+
21+
## Loop invariant to find all possiable window string
22+
23+
Assume that we had found a window string, `W`.
24+
25+
Then
26+
27+
1. When a new char comes, `W` + new char = `WN` is also a window. (Rule 1)
28+
1. Trim the new window string, `WN`, from the left part, remove any char that is redundant to have a window string. (Rule 2)
29+
30+
Code looks like
31+
32+
```
33+
34+
W = a window string in S
35+
36+
foreach c after W in S
37+
38+
WN = W + c
39+
40+
// Trim left
41+
42+
foreach _c in WN
43+
44+
if _c can be remove from WN
45+
remove _c from the left part of WN
46+
else
47+
break
48+
49+
W = WN
50+
51+
```
52+
53+
### Finding the first `W`
54+
55+
the `W` should have any char in `T`, this problem is like the `Check concatenation using count sum` in [Substring with Concatenation of All Words](../substring-with-concatenation-of-all-words).
56+
57+
Use a map `need`, stores how many chars are needed.
58+
59+
Use a map `seen`, stores how many chars are seen.
60+
61+
Those maps are not only used in finding `W`, but also in triming redundant from `WN`.
62+
63+
64+
This last problem is when the `W` is found.
65+
Like [Substring with Concatenation of All Words](../substring-with-concatenation-of-all-words), just use a `counter`,
66+
When `seen` is added, just add 1 to the counter.
67+
68+
The time `counter == T.length` is the time we first found `W`.
69+
70+
71+
72+
_Note_:
73+
a char in `seen[c]` can contribute at most `need[c]` count to the `counter`.
74+
75+
76+
77+
178

2-
## TODO
3-
* write down thinking
479

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,62 @@
1+
## Convert to [Regular Expression Matching](../regular-expression-matching)
12

2-
## TODO
3-
* write down thinking
3+
* `?` -> `.`
4+
* `*` -> `.*`
45

6+
This should work, yet, it exceeds the time limited.
7+
8+
## Checkpoint / Timemachine
9+
10+
On modern OS, you can go back to some time you saved if your OS meets some problem.
11+
such technology also can be used to solve this problem.
12+
13+
```
14+
S
15+
16+
[ a a b ]
17+
18+
[ a * b * ]
19+
20+
P
21+
22+
23+
P is *, save current state.
24+
```
25+
26+
`P` goes on to next
27+
28+
```
29+
30+
This state means * matches nothing
31+
32+
S
33+
34+
[ a a b ]
35+
36+
[ a * b * ]
37+
38+
P
39+
40+
```
41+
42+
a mismatch found, `P != S`, so rollback to checkpoint.
43+
44+
```
45+
This state means * matches a
46+
47+
S
48+
49+
[ a a b ]
50+
51+
[ a * b * ]
52+
53+
P
54+
```
55+
56+
Each `*` will save to a checkpoint, and retry from nothing until a match is found.
57+
58+
59+
### Remaining tail
60+
61+
Sometimes, `S` meets the end, but the `P` not.
62+
In this case, just to check remaining chars in `P` are all `*`.

minimum-window-substring/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
layout: solution
33
title: Minimum Window Substring
4-
date: 2014-07-23 02:42:48 +0800
4+
date: 2014-08-25 00:31:17 +0800
5+
eaten: true
56
---
67
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
78
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_root/' }} %}

wildcard-matching/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
layout: solution
33
title: Wildcard Matching
4-
date: 2014-07-23 02:42:48 +0800
4+
date: 2014-08-23 13:09:31 +0800
5+
eaten: true
56
---
67
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
78
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_root/' }} %}

0 commit comments

Comments
 (0)