GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — New Problem "Is Subsequence" · weimo/leetcode@ea1a213

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • algorithms/cpp/isSubsequence
    • IsSubsequence.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
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|
Collapse file

‎algorithms/cpp/isSubsequence/IsSubsequence.cpp‎

Copy file name to clipboard
+50Lines 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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.