Is Subsequence
Check whether one string can be made from another by deleting characters, without changing the order of the rest.
Given two strings sttruestfalset
Example 1
Output: true
Explanation: a, b and c appear in t at positions 0, 2 and 5.
Example 2
Output: false
Explanation: there is no x in t.
Example 3
Output: false
Explanation: every letter is in t, but x comes after c, not between a and c.
The Idea
Write the word "ahbgdc" on paper and cross out the h, the g and the d. The letters left over read "abc". Crossing out letters is all that deleting means here, and the letters that stay keep their places. So the question is whether we can cross out letters of ts
Example 2 fails for a plain reason: tfalse
So the order matters, and that raises a question. When we read ts
In the top half, labelled Example 1, the a of ststrue
In the bottom half, labelled Example 3, the a again takes position 0. The search for x has to pass the c at position 5 and finds x only at position 6, the last one. The c of sfalse
Notice that each letter took the first copy it met. That answers the question: taking the earliest copy is always safe, because it leaves as much of t
Solution Approach
Turning that idea into code means settling three things: how to keep track of both strings, why taking the earliest match never needs undoing, and when to stop. It also helps to see why trying every way of crossing out is slow, and how to handle many strings st
Trying every way of crossing out
Why not build every string that crossing out letters of tstt
Keeping track of both strings
What do we need to remember while reading tisjtji
Both positions only ever move forward, each through its own string. Walking two strings with one marker in each like this is a form of the two-pointer technique, where a pointer is simply a position in a string or array.
Why the earliest match is never wrong
Could taking the first copy of a letter ever cost us a match later? Suppose sst
When to stop
When is the answer known? If istjtsis
Optional: many strings s against one t
What if tstt
Then, for each letter of s
Java Solution
The method isSubsequence
- First, set
to 0. It is the position ini of the next letter to find.s - A
loop movesfor alongj , one letter per turn. It stops at the end oft , or as soon ast reachesi , because then every letter has been found.s.length() - Inside the loop, compare
withs.charAt(i) . If they are equal, the letter is found at its earliest place, sot.charAt(j) moves on to the next letter ofi .s - After the loop, return whether
, that is, whether every letter ofi == s.length() was found.s
The check i < s.length()s.charAt(i)s
The mainst
public class IsSubsequence {
public static boolean isSubsequence(String s, String t) {
int i = 0; // next character of s to find
for (int j = 0; j < t.length() && i < s.length(); j++) {
if (s.charAt(i) == t.charAt(j)) {
i++; // found it; look for the next one
}
}
return i == s.length();
}
public static void main(String[] args) {
System.out.println(isSubsequence("abc", "ahbgdc"));
System.out.println(isSubsequence("axc", "ahbgdc"));
System.out.println(isSubsequence("axc", "ahbgdcx"));
System.out.println(isSubsequence("ba", "abc"));
System.out.println(isSubsequence("aab", "ab"));
System.out.println(isSubsequence("aab", "abab"));
System.out.println(isSubsequence("", "abc"));
System.out.println(isSubsequence("a", ""));
}
}
Running it prints:
true
false
false
false
false
true
true
false
The answers truefalsefalsefalse
Lines five and six test a repeated letter. "aab" needs two a's, and "ab" has only one, so the fifth line is falsetrue
The last two lines are the empty cases. An empty strueistfalse
The original implementation,
Complexity
The work is a single left-to-right read of t
- Time: O(n) for a
of n letters, becauset visits each letter ofj at most once, and the loop may stop earlier, as soon as the last letter oft is found.s - Space: O(1). The method keeps only the two positions
andi , with no copies of the strings and no lookup tables.j