-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisSubsequence.js
More file actions
28 lines (27 loc) · 1.01 KB
/
isSubsequence.js
File metadata and controls
28 lines (27 loc) · 1.01 KB
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
/* Write a function called isSubsequence which takes in two strings and checks whether the characters in the
* first string form a subsequence of the characters in the second string. In other words, the function
* should check whether the characters in the first string appear somewhere in the second string, without their order changing
* Your solution MUST have AT LEAST the following complexities:
* Time Complexity - O(N + M)
* Space Complexity - O(1) */
const isSubsequence = (firstString, secondString) => {
let i = 0;
let j = 0;
while (j < secondString.length) {
if (firstString[i] === secondString[j]) {
if (firstString[i + 1] === undefined) {
return true;
} else {
i++;
j++;
}
} else {
j++;
}
}
return false;
}
console.log(isSubsequence('hello', 'hello world')); // true
console.log(isSubsequence('sing', 'sting')); // true
console.log(isSubsequence('abc', 'abracadabra')); // true
console.log(isSubsequence('abc', 'acb')); // false (order matters)