Problem: Given a string s and a string t, check if s is subsequence of t.
Example:
Input: s = "nsa", t = "nishant" Output: true
Input: s = "nas", t = "nishant" Output: false
Approach: It's a straight forward problem to solve. Solution can be easily understood by just looking at the code.
Implementation in C#:
public bool IsSubsequence(string s, string t)
{
int sItr = 0, tItr = 0;
while (sItr < s.Length && tItr < t.Length)
{
if (s[sItr] == t[tItr])
{
++sItr;
}
++tItr;
}
return sItr == s.Length;
}
Complexity: O(n)
No comments:
Post a Comment