-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path392 is Subsequence
47 lines (38 loc) · 1.37 KB
/
392 is Subsequence
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
// 392. Is Subsequence
// Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
// A subsequence of a string is a new string that is formed from the original string by deleting some
// (can be none) of the characters without disturbing the relative positions of the remaining characters.
// (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isSubsequence = function(s, t)
{
// instatiate pointer that will traverse
// both strings.
let i = 0;
let j = 0;
// while neither one of our pointers exceed
// the length of the strings they are
// overseeing
while( j < t.length && i < s.length)
{
// if s at the i index is equal to the
// j index of t, we want to incriment the
// value of i. If not, we want to continue
// to check t to see if the value for which
// we are searching exists in the order we
// need
if(s[i] === t[j]) {
i++;
}
j++;
}
// if s is a subsequence of t, the length of s should
// be equal to the whole number of i being that i will
// only increment if s is found inside of t in the exact
// order that it appears as in s
return i === s.length
};