Skip to content

Commit 1ad9691

Browse files
authored
Add c# solution
1 parent 8faeefe commit 1ad9691

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
public class Solution {
2+
3+
// A faster method
4+
public bool IsSubsequence(string s, string t) {
5+
if (s == "")
6+
return true;
7+
8+
if (s.Length > t.Length)
9+
return false;
10+
11+
int inds = 0;
12+
foreach (char c in t) {
13+
if (s[inds] == c) {
14+
inds++;
15+
if (inds == s.Length)
16+
return true;
17+
}
18+
}
19+
return false;
20+
}
21+
22+
// A more general method
23+
public bool IsSubsequenceMine(string s, string t) {
24+
25+
if (s.Length > t.Length)
26+
return false;
27+
28+
// the following comparison makes the method faster
29+
if (s.Length == t.Length)
30+
return s == t;
31+
32+
int inds = 0, indt = 0;
33+
while (inds < s.Length && indt < t.Length) {
34+
if (s[inds] == t[indt]) {
35+
inds++;
36+
}
37+
indt++;
38+
}
39+
return inds > s.Length-1;
40+
}
41+
}

0 commit comments

Comments
 (0)