Skip to content

Commit 1916607

Browse files
committed
Add is subsequence
1 parent 9ddb31e commit 1916607

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Is Subsequence
2+
3+
## Problem Description
4+
5+
Given two strings `s` and `t`, return `true` if `s` is a subsequence of `t`, or `false` otherwise.
6+
7+
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).
8+
9+
**Example 1:**
10+
11+
Input: `s = "abc"`, `t = "ahbgdc"`
12+
Output: `true`
13+
14+
**Example 2:**
15+
16+
Input: `s = "axc"`, `t = "ahbgdc"`
17+
Output: `false`
18+
19+
**Constraints:**
20+
21+
* `0 <= s.length <= 100`
22+
* `0 <= t.length <= 10^4`
23+
* `s` and `t` consist only of lowercase English letters.
24+
25+
## Solution
26+
27+
```python
28+
def is_subsequence(self, s: str, t: str) -> bool:
29+
"""
30+
Check if string `s` is a subsequence of string `t`.
31+
32+
A subsequence is a sequence that can be derived from another sequence by deleting zero or more elements
33+
without changing the order of the remaining elements.
34+
35+
:param self: The instance reference (for method implementation)
36+
:param s: The potential subsequence string to check
37+
:param t: The string in which to search for the subsequence
38+
:return: True if `s` is a subsequence of `t`, False otherwise
39+
"""
40+
s_pointer = 0
41+
s_length = len(s)
42+
43+
if not s_length:
44+
return True
45+
46+
for t_pointer in range(len(t)):
47+
if s[s_pointer] == t[t_pointer]:
48+
s_pointer += 1
49+
if s_pointer == s_length:
50+
return True
51+
52+
return False
53+
```
54+
55+
* **Time Complexity:** $O(n)$
56+
* **Space Complexity:** $O(1)$
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def is_subsequence(self, s: str, t: str) -> bool:
2+
"""
3+
Check if string `s` is a subsequence of string `t`.
4+
5+
A subsequence is a sequence that can be derived from another sequence by deleting zero or more elements
6+
without changing the order of the remaining elements.
7+
8+
:param self: The instance reference (for method implementation)
9+
:param s: The potential subsequence string to check
10+
:param t: The string in which to search for the subsequence
11+
:return: True if `s` is a subsequence of `t`, False otherwise
12+
"""
13+
s_pointer = 0
14+
s_length = len(s)
15+
16+
if not s_length:
17+
return True
18+
19+
for t_pointer in range(len(t)):
20+
if s[s_pointer] == t[t_pointer]:
21+
s_pointer += 1
22+
if s_pointer == s_length:
23+
return True
24+
25+
return False
26+
27+
28+
assert is_subsequence(s='abc', t='ahbgdc') == True, 'Test 1 Failed'
29+
assert is_subsequence(s='axc', t='ahbgdc') == False, 'Test 2 Failed'
30+
assert is_subsequence(s='', t='ahbgdc') == True, 'Test 3 Failed'

0 commit comments

Comments
 (0)