Skip to content

Commit 6368807

Browse files
committedDec 28, 2023
Add: Exercise 392. Is Subsequence
1 parent a27c291 commit 6368807

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed
 

‎392-is-subsequence/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 392. Is Subsequence
2+
Given two strings `s` and `t`, return `true` if `s` is a subsequence of `t`, or `false` otherwise.
3+
4+
A subsequence of a string is a new string that is formed from the original string by deleting some
5+
(can be none) of the characters without disturbing the relative positions of the remaining
6+
characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
7+
8+
## Examples
9+
10+
### Example 1:
11+
Input: `s` = "abc", `t` = "ahbgdc"
12+
Output: `true`
13+
14+
### Example 2:
15+
Input: `s` = "axc", `t` = "ahbgdc"
16+
Output: `false`
17+
18+
## Constraints
19+
- 0 <= `s.length` <= 100
20+
- 0 <= `t.length` <= 10^4
21+
- `s` and `t` consist only of lowercase English letters.
22+
23+
## Follow up
24+
Suppose there are lots of incoming `s`, say `s1`, `s2`, ..., `sk` where `k` >= 10^9, and you want to
25+
check one by one to see if `t` has its subsequence. In this scenario, how would you change your
26+
code?

‎392-is-subsequence/index.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Determines if `s` is a subsequence of `t`. A subsequence is formed by deleting some or no
3+
* elements from a string without changing the order of the remaining elements. This function
4+
* iterates through `t` and compares it with `s`, increasing a pointer for each match.
5+
* If all characters of `s` are found in sequence in `t`, `s` is a subsequence of `t`.
6+
*
7+
* @param {string} s - String to check as a subsequence.
8+
* @param {string} t - String to be checked against.
9+
* @return {boolean} - True if `s` is a subsequence of `t`, false otherwise.
10+
*/
11+
12+
var isSubsequence = function(s, t) {
13+
// Traverse t and advance i when s[i] is found
14+
let i = 0 // Index for iterate over t
15+
let j = 0 // Index for iterate over s
16+
17+
// Edge case
18+
if (s.length > t.length) {
19+
return false
20+
}
21+
22+
while (i < t.length && j < s.length) {
23+
if (s[j] === t[i]) {
24+
j++
25+
}
26+
i++
27+
}
28+
29+
return j === s.length
30+
};
31+
32+
module.exports = isSubsequence

‎392-is-subsequence/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "392-is-subsequence",
3+
"version": "1.0.0",
4+
"description": "Given two strings `s` and `t`, return `true` if `s` is a subsequence of `t`, or `false` otherwise.",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"debug": "node --inspect-brk index.js",
9+
"test": "node test.js"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC"
14+
}

‎392-is-subsequence/test.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const assert = require('assert')
2+
const isSubsequence = require('./index')
3+
4+
const tt = [
5+
{
6+
s: 'abc',
7+
t: 'ahbgdc',
8+
expected: true,
9+
},
10+
{
11+
s: 'axc',
12+
t: 'ahbgdc',
13+
expected: false,
14+
}
15+
]
16+
17+
for (const t of tt) {
18+
assert.strict.equal(isSubsequence(t.s, t.t), t.expected)
19+
}

0 commit comments

Comments
 (0)
Please sign in to comment.