-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path392_is_subsequence.go
More file actions
74 lines (69 loc) · 1.93 KB
/
392_is_subsequence.go
File metadata and controls
74 lines (69 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package leetcode
// https://leetcode.com/problems/is-subsequence/
// Key point is the character has to appear in the right order
// for
// "abc"
// "xxxbxxxcxxaxxxbxc"
// when we find "a" in string 't' at position 'i', the next position we need to search is 'i+1'
func isSubsequence(s string, t string) bool {
if len(t) < len(s) {
return false
}
sIndex := 0
for _, tChar := range t {
if sIndex >= len(s) {
return true
}
if rune(s[sIndex]) == tChar {
sIndex++
}
}
return sIndex == len(s)
}
// Follow up: Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 109,
// and you want to check one by one to see if t has its subsequence.
// In this scenario, how would you change your code?
// In this follow up, since there will be high amounts of 's', and 's' is more often smaller than 't'
// It will be beneficial to 'cache' what we know of 't'
// Here i'm caching the index of all the letters
//
// Another way to reduce the for loop when checking the index is use a 2D array of letters to position
// x\i :0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
// a | 1 | 1 | 1 | 1 | 2 | 2 | 2 | 2 |
// b | 0 | 1 | 1 | 1 | 1 | 2 | 2 | 2 |
// c | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
// d | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 2 |
// e | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 |
// ...
// z | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
func isSubsequenceMulti(s string, t string) bool {
if len(t) < len(s) {
return false
}
// this can be a global
tJump := make(map[rune][]int)
for tIndex, tChar := range t {
tJump[tChar] = append(tJump[tChar], tIndex)
}
// this part is what should be called multiple times
isSubOfT := func(s string) bool {
prevIndex := -1
for _, sChar := range s {
if len(tJump[sChar]) == 0 {
return false
}
tmp := prevIndex
for i, nextIndex := range tJump[sChar] {
if nextIndex > prevIndex {
prevIndex = tJump[sChar][i]
break
}
}
if tmp == prevIndex {
return false
}
}
return true
}
return isSubOfT(s)
}