Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ca10736

Browse files
Add solution for Is Subsequence problem with comprehensive test cases
1 parent e56abcc commit ca10736

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- https://leetcode.com/problems/contains-duplicate-ii/description/
2727
- https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/
2828
- https://leetcode.com/problems/three-consecutive-odds/description/?envType=daily-question&envId=2025年05月11日
29+
- String Problem: https://leetcode.com/problems/is-subsequence/description/
2930

3031

3132

‎leetcode/392.is_subsequence.dart

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// https://leetcode.com/problems/is-subsequence/description/
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
final stopwatch = Stopwatch()..start();
6+
runTests();
7+
8+
///
9+
stopwatch.stop();
10+
print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro sec');
11+
}
12+
13+
class Solution {
14+
15+
////! Done from first time TC: O(n)
16+
bool isSubsequence(String s, String t) {
17+
if (s.isEmpty) return true;
18+
if (s.isEmpty) return false;
19+
20+
int sIndex = 0;
21+
for (var i = 0; i < t.length; i++) {
22+
if (s[sIndex] == t[i]) {
23+
sIndex++;
24+
if (sIndex >= s.length) return true;
25+
}
26+
}
27+
28+
return false;
29+
}
30+
}
31+
32+
void runTests() {
33+
final Solution s = Solution();
34+
35+
group('Is Subsequence', () {
36+
// Basic examples from problem statement
37+
test('Example 1: s="abc", t="ahbgdc" → true', () {
38+
expect(s.isSubsequence("abc", "ahbgdc"), isTrue);
39+
});
40+
41+
test('Example 2: s="axc", t="ahbgdc" → false', () {
42+
expect(s.isSubsequence("axc", "ahbgdc"), isFalse);
43+
});
44+
45+
// Edge cases
46+
test('Empty s: s="", t="ahbgdc" → true', () {
47+
expect(s.isSubsequence("", "ahbgdc"), isTrue);
48+
});
49+
50+
test('Empty t: s="abc", t="" → false', () {
51+
expect(s.isSubsequence("abc", ""), isFalse);
52+
});
53+
54+
test('Both empty: s="", t="" → true', () {
55+
expect(s.isSubsequence("", ""), isTrue);
56+
});
57+
58+
// Single character cases
59+
test('Single char match: s="a", t="abc" → true', () {
60+
expect(s.isSubsequence("a", "abc"), isTrue);
61+
});
62+
63+
test('Single char no match: s="d", t="abc" → false', () {
64+
expect(s.isSubsequence("d", "abc"), isFalse);
65+
});
66+
67+
// Exact match
68+
test('Exact match: s="abc", t="abc" → true', () {
69+
expect(s.isSubsequence("abc", "abc"), isTrue);
70+
});
71+
72+
// Non-consecutive matches
73+
test('Non-consecutive match: s="ace", t="abcde" → true', () {
74+
expect(s.isSubsequence("ace", "abcde"), isTrue);
75+
});
76+
77+
test('Non-consecutive no match: s="aec", t="abcde" → false', () {
78+
expect(s.isSubsequence("aec", "abcde"), isFalse);
79+
});
80+
81+
// Repeated characters
82+
test('Repeated chars match: s="aabb", t="aaabbb" → true', () {
83+
expect(s.isSubsequence("aabb", "aaabbb"), isTrue);
84+
});
85+
86+
test('Repeated chars no match: s="aabb", t="ababab" → false', () {
87+
expect(s.isSubsequence("aabb", "ababab"), isTrue);
88+
});
89+
90+
// Large strings
91+
test('Large t string with match', () {
92+
final t = '${List.generate(10000, (i) => 'a').join()}b';
93+
expect(s.isSubsequence("ab", t), isTrue);
94+
});
95+
96+
test('Large t string without match', () {
97+
final t = List.generate(10000, (i) => 'a').join();
98+
expect(s.isSubsequence("b", t), isFalse);
99+
});
100+
101+
// s longer than t
102+
test('s longer than t: s="abcd", t="abc" → false', () {
103+
expect(s.isSubsequence("abcd", "abc"), isFalse);
104+
});
105+
106+
// Maximum constraints
107+
test('Maximum length t with early match', () {
108+
final t = 'z${List.generate(9999, (i) => 'a').join()}';
109+
expect(s.isSubsequence("za", t), isTrue);
110+
});
111+
112+
// Special cases
113+
test('All same characters: s="aaa", t="aaab" → true', () {
114+
expect(s.isSubsequence("aaa", "aaab"), isTrue);
115+
});
116+
117+
test('All same characters no match: s="aaaa", t="aaa" → false', () {
118+
expect(s.isSubsequence("aaaa", "aaa"), isFalse);
119+
});
120+
121+
// Random cases
122+
test('Random case 1: s="hello", t="hheelllloo" → true', () {
123+
expect(s.isSubsequence("hello", "hheelllloo"), isTrue);
124+
});
125+
126+
test('Random case 2: s="world", t="wrrroldd" → false', () {
127+
expect(s.isSubsequence("world", "wrrroldd"), isFalse);
128+
});
129+
});
130+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /