-
Notifications
You must be signed in to change notification settings - Fork 1
/
isSubsquent.js
39 lines (36 loc) · 924 Bytes
/
isSubsquent.js
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
/**
* write a function which takes two strings and checks whether the charac. in the first string form a
* subsequence of the char. in the second string. In order words, the function should check whether the
* char. in the first string appear somewhere in the second string.
*/
function isSubsequence(str1, str2) {
let i = 0;
let j = 0;
if(!str1) return true;
while (i < str1.length && j < str2.length) {
if (str1[i] === str2[j])
i++;
j++
if (i === str1.length)
return true
}
return false
}
const result = isSubsequence('hello', 'hrrrr')
console.log(result);
/**
* 'abc' 'dfxzrabc'
* i j
*/
/**
* i = 0
* j = 1
* if(!str1) return true
* while the i < str1.length && j < str2.length
* if str1[i] === str2[j]
* i++
* j++
* if i === str1.length
* return true
* return false
*/