Skip to content

Commit 9bf836f

Browse files
authoredDec 15, 2018
Create 392-is-subsequence.js
1 parent 5f9f337 commit 9bf836f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
 

‎392-is-subsequence.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
const isSubsequence = function(s, t) {
7+
let ti = 0
8+
let tmp = 0
9+
for (let i = 0; i < s.length; i++) {
10+
if ((tmp = chk(t, ti, s.charAt(i))) === -1) {
11+
return false
12+
} else {
13+
ti = tmp + 1
14+
}
15+
}
16+
17+
return true
18+
}
19+
20+
function chk(str, start, target) {
21+
let idx = start
22+
for (let i = start; i < str.length; i++) {
23+
if (str.charAt(i) === target) {
24+
return i
25+
}
26+
}
27+
return -1
28+
}

0 commit comments

Comments
 (0)
Please sign in to comment.