-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisSubsequence.cs
30 lines (29 loc) · 1.04 KB
/
isSubsequence.cs
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
public class Solution {
public bool IsSubsequence(string s, string t) {
string slice = t;
int sCounter = 0;
//Counter variable for how many characters we have found.
if(String.IsNullOrEmpty(s))
{
return true;
//Break out on null inputs.
}
for(int i = 0; i < t.Length; i++)
{
if(t[i] == s[sCounter])
{
sCounter++;
//counter represents the character in s we're looking for, i represents the index in t that we're at.
//If we find it, we increment sCounter.
}
if(sCounter >= s.Length)
{
return true;
//If sCounter *ever* hits the length of s - we know this is true and break here (the way we add to sCount requires
//the correct order.
}
}
return false;
}
}
//Written by Connor Matza for problem https://leetcode.com/problems/is-subsequence/submissions/ runtime 116ms passing 14 text cases.