forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissubsequence.go
More file actions
35 lines (28 loc) · 742 Bytes
/
issubsequence.go
File metadata and controls
35 lines (28 loc) · 742 Bytes
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
// Checks if a given string is a subsequence of another string.
// A subsequence of a given string is a string that can be derived from the given
// string by deleting some or no characters without changing the order of the
// remaining characters. (i.e., "dpr" is a subsequence of "depqr" while "drp" is not).
// Author: sanjibgirics
package strings
// Returns true if s is subsequence of t, otherwise return false.
func IsSubsequence(s string, t string) bool {
if len(s) > len(t) {
return false
}
if s == t {
return true
}
if len(s) == 0 {
return true
}
sIndex := 0
for tIndex := 0; tIndex < len(t); tIndex++ {
if s[sIndex] == t[tIndex] {
sIndex++
}
if sIndex == len(s) {
return true
}
}
return false
}