Skip to content

Commit ac1361e

Browse files
committed
add solution of problem 392: is subsequence
1 parent 7b5642b commit ac1361e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

IsSubsequence392/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Given a string **s** and a string **t**, check if **s** is subsequence of **t**.
2+
3+
You may assume that there is only lower case English letters in both **s** and **t**. **t** is potentially a very long (length ~= 500,000) string, and **s** is a short string (<=100).
4+
5+
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, `"ace"` is a subsequence of `"abcde"` while `"aec"` is not).
6+
7+
####Example 1:
8+
**s** = `"abc"`, **t** = `"ahbgdc"`
9+
10+
Return `true`.
11+
12+
####Example 2:
13+
s = `"axc"`, t = `"ahbgdc"`
14+
15+
Return `false`.
16+
17+
####Follow up:
18+
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?

IsSubsequence392/Solution.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Solution {
2+
public boolean isSubsequence(String s, String t) {
3+
if (s == null && t == null)
4+
return true;
5+
else if (s == null || t == null)
6+
return false;
7+
8+
int idxOfS = 0;
9+
for (int idxOfT = 0; idxOfT < t.length() && idxOfS < s.length(); idxOfT++) {
10+
if (s.charAt(idxOfS) == t.charAt(idxOfT))
11+
idxOfS++;
12+
}
13+
14+
return idxOfS == s.length();
15+
}
16+
}

0 commit comments

Comments
 (0)