Skip to content

Commit f74ee49

Browse files
committed
feat: solve No.392
1 parent 8068915 commit f74ee49

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

301-400/392. Is Subsequence.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 392. Is Subsequence
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Two Pointers, String, Dynamic Programming.
5+
- Similar Questions: Number of Matching Subsequences, Shortest Way to Form String, Append Characters to String to Make Subsequence, Make String a Subsequence Using Cyclic Increments.
6+
7+
## Problem
8+
9+
Given two strings `s` and `t`, return `true`** if **`s`** is a **subsequence** of **`t`**, or **`false`** otherwise**.
10+
11+
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).
12+
13+
 
14+
Example 1:
15+
```
16+
Input: s = "abc", t = "ahbgdc"
17+
Output: true
18+
```Example 2:
19+
```
20+
Input: s = "axc", t = "ahbgdc"
21+
Output: false
22+
```
23+
 
24+
**Constraints:**
25+
26+
27+
28+
- `0 <= s.length <= 100`
29+
30+
- `0 <= t.length <= 104`
31+
32+
- `s` and `t` consist only of lowercase English letters.
33+
34+
35+
 
36+
**Follow up:** Suppose there are lots of incoming `s`, say `s1, s2, ..., sk` where `k >= 109`, and you want to check one by one to see if `t` has its subsequence. In this scenario, how would you change your code?
37+
38+
## Solution
39+
40+
```javascript
41+
/**
42+
* @param {string} s
43+
* @param {string} t
44+
* @return {boolean}
45+
*/
46+
var isSubsequence = function(s, t) {
47+
var j = 0;
48+
for (var i = 0; i < s.length; i++) {
49+
while (s[i] !== t[j] && j < t.length - 1) j++;
50+
if (s[i] != t[j]) return false;
51+
j++;
52+
}
53+
return true;
54+
};
55+
```
56+
57+
**Explain:**
58+
59+
nope.
60+
61+
**Complexity:**
62+
63+
* Time complexity : O(n).
64+
* Space complexity : O(1).

0 commit comments

Comments
 (0)