Skip to content

Commit 3cd3fe3

Browse files
committed
Add solution for is subsequence
1 parent 29c7b39 commit 3cd3fe3

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

two-pointer/is-subsequence/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# 443. String Compression
2+
3+
## Description
4+
See https://leetcode.com/problems/string-compression/description/
5+
6+
## Problem
7+
Given an array of characters `chars`, compress it using the following algorithm:
8+
9+
Begin with an empty string `s`. For each group of consecutive repeating characters in `chars`:
10+
11+
If the group's length is `1`, append the character to `s`.
12+
Otherwise, append the character followed by the group's length.
13+
The compressed string `s` should not be returned separately, but instead, be stored in the input character array `chars`. Note that group lengths that are `10` or longer will be split into multiple characters in chars.
14+
15+
After you are done modifying the input array, return the new length of the array.
16+
17+
You must write an algorithm that uses only constant extra space.
18+
19+
## Example 1
20+
21+
```
22+
Input: chars = ["a","a","b","b","c","c","c"]
23+
Output: Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
24+
Explanation: The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".
25+
```
26+
27+
## Example 2
28+
29+
```
30+
Input: chars = ["a"]
31+
Output: Return 1, and the first character of the input array should be: ["a"]
32+
Explanation: The only group is "a", which remains uncompressed since it's a single character.
33+
```
34+
35+
## Example 3
36+
37+
```
38+
Input: chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
39+
Output: Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
40+
Explanation: The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".
41+
```
42+
43+
## Constraints
44+
45+
```
46+
1 <= chars.length <= 2000
47+
chars[i] is a lowercase English letter, uppercase English letter, digit, or symbol.
48+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def is_subsequence(s: str, t: str) -> bool:
2+
i, j = 0, 0
3+
while i < len(s) and j < len(t):
4+
if s[i] == t[j]:
5+
i += 1
6+
j += 1
7+
return i == len(s)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from subsequence import is_subsequence
2+
3+
def test_example_1():
4+
s = "abc"
5+
t = "ahbgdc"
6+
assert is_subsequence(s, t) == True
7+
8+
def test_example_2():
9+
s = "axc"
10+
t = "ahbgdc"
11+
assert is_subsequence(s, t) == False
12+
13+
def test_example_3():
14+
s = ""
15+
t = "ahbgdc"
16+
assert is_subsequence(s, t) == True
17+
18+
def test_example_4():
19+
s = "abc"
20+
t = ""
21+
assert is_subsequence(s, t) == False
22+
23+
def test_example_5():
24+
s = "ace"
25+
t = "abcde"
26+
assert is_subsequence(s, t) == True

0 commit comments

Comments
 (0)