GeezerVison is showing github.com — New Problem "Is Subsequence" · weimo/leetcode@ea1a213
File tree
Expand file treeCollapse file tree
Open diff view settings
algorithms/cpp/isSubsequence
Expand file treeCollapse file tree
Open diff view settings
Collapse file
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 number | Diff line number | Diff line change |
|---|
|
8 | 8 |
|
9 | 9 | | # | Title | Solution | Difficulty | |
10 | 10 | |---| ----- | -------- | ---------- | |
| 11 | +|392|[Is Subsequence](https://leetcode.com/problems/is-subsequence/) | [C++](./algorithms/cpp/isSubsequence/IsSubsequence.cpp)|Medium| |
11 | 12 | |391|[Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/) | [C++](./algorithms/cpp/perfectRectangle/PerfectRectangle.cpp)|Hard| |
12 | 13 | |390|[Elimination Game](https://leetcode.com/contest/2/problems/elimination-game/) | [C++](./algorithms/cpp/eliminationGame/EliminationGame.cpp)|Medium| |
13 | 14 | |389|[Find the Difference](https://leetcode.com/problems/find-the-difference/) | [C++](./algorithms/cpp/findTheDifference/FindTheDifference.cpp)|Easy| |
|
Collapse file
Copy file name to clipboard +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 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 | +}; |