Skip to content

Commit ea1a213

Browse files
committed
New Problem "Is Subsequence"
1 parent 626efee commit ea1a213

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LeetCode
88

99
| # | Title | Solution | Difficulty |
1010
|---| ----- | -------- | ---------- |
11+
|392|[Is Subsequence](https://leetcode.com/problems/is-subsequence/) | [C++](./algorithms/cpp/isSubsequence/IsSubsequence.cpp)|Medium|
1112
|391|[Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/) | [C++](./algorithms/cpp/perfectRectangle/PerfectRectangle.cpp)|Hard|
1213
|390|[Elimination Game](https://leetcode.com/contest/2/problems/elimination-game/) | [C++](./algorithms/cpp/eliminationGame/EliminationGame.cpp)|Medium|
1314
|389|[Find the Difference](https://leetcode.com/problems/find-the-difference/) | [C++](./algorithms/cpp/findTheDifference/FindTheDifference.cpp)|Easy|
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Source : https://leetcode.com/problems/is-subsequence/
2+
// Author : Hao Chen
3+
// Date : 2016-09-08
4+
5+
/***************************************************************************************
6+
*
7+
* Given a string s and a string t, check if s is subsequence of t.
8+
*
9+
* You may assume that there is only lower case English letters in both s and t. t is
10+
* potentially a very long (length ~= 500,000) string, and s is a short string (
11+
*
12+
* A subsequence of a string is a new string which is formed from the original string
13+
* by deleting some (can be none) of the characters without disturbing the relative
14+
* positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while
15+
* "aec" is not).
16+
*
17+
* Example 1:
18+
* s = "abc", t = "ahbgdc"
19+
*
20+
* Return true.
21+
*
22+
* Example 2:
23+
* s = "axc", t = "ahbgdc"
24+
*
25+
* Return false.
26+
*
27+
* Follow up:
28+
* If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to
29+
* check one by one to see if T has its subsequence. In this scenario, how would you
30+
* change your code?
31+
***************************************************************************************/
32+
33+
class Solution {
34+
public:
35+
bool isSubsequence(string s, string t) {
36+
if (s.size() <= 0) return true;
37+
38+
int ps=0, pt=0;
39+
while (pt < t.size()) {
40+
if (s[ps] == t[pt]) {
41+
ps++; pt++;
42+
if (ps >= s.size()) return true;
43+
}else {
44+
pt++;
45+
}
46+
}
47+
48+
return false;
49+
}
50+
};

0 commit comments

Comments
 (0)