-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis-subsequence.go
57 lines (46 loc) · 1.16 KB
/
is-subsequence.go
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
package main
import "fmt"
// source: https://leetcode.com/problems/is-subsequence/
func isSubsequence(s string, t string) bool {
var sp, tp int
var sl, tl = len(s), len(t)
if sl > tl {
return false
}
for ; sp < sl && tp < tl; tp++ {
if s[sp] == t[tp] {
sp++
}
}
return sp == sl
}
func main() {
// Example 1
var s1 string = "abc"
var t1 string = "ahbgdc"
fmt.Println("Expected: true Output: ", isSubsequence(s1, t1))
// Example 2
var s2 string = "axc"
var t2 string = "ahbgdc"
fmt.Println("Expected: false Output: ", isSubsequence(s2, t2))
// Example 3
var s3 string = ""
var t3 string = "a"
fmt.Println("Expected: true Output: ", isSubsequence(s3, t3))
// Example 4
var s4 string = ""
var t4 string = ""
fmt.Println("Expected: true Output: ", isSubsequence(s4, t4))
// Example 5
var s5 string = "aa"
var t5 string = "a"
fmt.Println("Expected: false Output: ", isSubsequence(s5, t5))
// Example 6
var s6 string = "abc"
var t6 string = "aaabbbccc"
fmt.Println("Expected: true Output: ", isSubsequence(s6, t6))
// Example 7
var s7 string = "x"
var t7 string = "xxxxxxx"
fmt.Println("Expected: true Output: ", isSubsequence(s7, t7))
}