Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
Follow up: Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 10^9, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?
0 <= s.length <= 1000 <= t.length <= 10^4s and t consist only of lowercase English letters.No developer has claimed the Python throne for LeetCode #392 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
t[i] and s[j].t, i is automatically incremented by 1, and if t[i] == s[j], then j += 1.j >= s.length, return true. If t is traversed and still not returned in advance, return false.class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if s == "":
return True
s_index = 0
for i in range(len(t)):
if t[i] == s[s_index]:
s_index += 1
if s_index >= len(s):
return True
return False
Solution 1 is essentially a "dynamic programming" algorithm implemented with rolling variables. It is easy to understand, and the space complexity is reduced to O(1).Because it can handle a more complex scenario (e.g. 583. Delete Operation for Two Strings) that is common in dynamic programming.
one-dimensional rolling array to implement the "dynamic programming" algorithm?
Of course, but considering that for this problem a one-dimensional rolling array is not as easy to understand as a two-dimensional array, and the implementation process is also prone to errors, so here I didn't give the relevant code implementation. If you are interested, you can try it.
two-dimensional arrays for dynamic programming.class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
column_count = len(t) + 1
dp = [[True] * column_count]
for _ in s:
dp.append([False] * column_count)
for i in range(1, len(dp)):
for j in range(1, len(dp[0])):
if s[i - 1] == t[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = dp[i][j - 1]
return dp[-1][-1]
No developer has claimed the C++ throne for LeetCode #392 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
t[i] and s[j].t, i is automatically incremented by 1, and if t[i] == s[j], then j += 1.j >= s.length, return true. If t is traversed and still not returned in advance, return false.class Solution {
public:
bool isSubsequence(string s, string t) {
if (s.empty()) {
return true;
}
int s_index = 0;
for (int i = 0; i < t.length(); i++) {
if (t[i] == s[s_index]) {
s_index++;
if (s_index >= s.length()) {
return true;
}
}
}
return false;
}
};
Solution 1 is essentially a "dynamic programming" algorithm implemented with rolling variables. It is easy to understand, and the space complexity is reduced to O(1).Because it can handle a more complex scenario (e.g. 583. Delete Operation for Two Strings) that is common in dynamic programming.
one-dimensional rolling array to implement the "dynamic programming" algorithm?
Of course, but considering that for this problem a one-dimensional rolling array is not as easy to understand as a two-dimensional array, and the implementation process is also prone to errors, so here I didn't give the relevant code implementation. If you are interested, you can try it.
two-dimensional arrays for dynamic programming.class Solution {
public:
bool isSubsequence(string s, string t) {
vector<vector<bool>> dp(s.size() + 1, vector<bool>(t.size() + 1));
fill(dp[0].begin(), dp[0].end(), true);
for (auto i = 1; i < dp.size(); i++) {
for (auto j = 1; j < dp[0].size(); j++) {
if (s[i - 1] == t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = dp[i][j - 1];
}
}
}
return dp[dp.size() - 1][dp[0].size() - 1];
}
};
No developer has claimed the Java throne for LeetCode #392 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
t[i] and s[j].t, i is automatically incremented by 1, and if t[i] == s[j], then j += 1.j >= s.length, return true. If t is traversed and still not returned in advance, return false.class Solution {
public boolean isSubsequence(String s, String t) {
if (s.isEmpty()) {
return true;
}
int sIndex = 0;
for (int i = 0; i < t.length(); i++) {
if (t.charAt(i) == s.charAt(sIndex)) {
sIndex++;
if (sIndex >= s.length()) {
return true;
}
}
}
return false;
}
}
Solution 1 is essentially a "dynamic programming" algorithm implemented with rolling variables. It is easy to understand, and the space complexity is reduced to O(1).Because it can handle a more complex scenario (e.g. 583. Delete Operation for Two Strings) that is common in dynamic programming.
one-dimensional rolling array to implement the "dynamic programming" algorithm?
Of course, but considering that for this problem a one-dimensional rolling array is not as easy to understand as a two-dimensional array, and the implementation process is also prone to errors, so here I didn't give the relevant code implementation. If you are interested, you can try it.
two-dimensional arrays for dynamic programming.class Solution {
public boolean isSubsequence(String s, String t) {
var dp = new boolean[s.length() + 1][t.length() + 1];
Arrays.fill(dp[0], true);
for (var i = 1; i < dp.length; i++) {
for (var j = 1; j < dp[0].length; j++) {
if (s.charAt(i - 1) == t.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = dp[i][j - 1];
}
}
}
return dp[dp.length - 1][dp[0].length - 1];
}
}
No developer has claimed the C# throne for LeetCode #392 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
t[i] and s[j].t, i is automatically incremented by 1, and if t[i] == s[j], then j += 1.j >= s.length, return true. If t is traversed and still not returned in advance, return false.public class Solution
{
public bool IsSubsequence(string s, string t)
{
if (string.IsNullOrEmpty(s))
{
return true;
}
int sIndex = 0;
for (int i = 0; i < t.Length; i++)
{
if (t[i] == s[sIndex])
{
sIndex++;
if (sIndex >= s.Length)
{
return true;
}
}
}
return false;
}
}
Solution 1 is essentially a "dynamic programming" algorithm implemented with rolling variables. It is easy to understand, and the space complexity is reduced to O(1).Because it can handle a more complex scenario (e.g. 583. Delete Operation for Two Strings) that is common in dynamic programming.
one-dimensional rolling array to implement the "dynamic programming" algorithm?
Of course, but considering that for this problem a one-dimensional rolling array is not as easy to understand as a two-dimensional array, and the implementation process is also prone to errors, so here I didn't give the relevant code implementation. If you are interested, you can try it.
two-dimensional arrays for dynamic programming.public class Solution
{
public bool IsSubsequence(string s, string t)
{
var dp = new bool[s.Length + 1, t.Length + 1];
for (var j = 0; j < dp.GetLength(1); j++)
dp[0, j] = true;
for (var i = 1; i < dp.GetLength(0); i++)
{
for (var j = 1; j < dp.GetLength(1); j++)
{
if (s[i - 1] == t[j - 1])
{
dp[i, j] = dp[i - 1, j - 1];
}
else
{
dp[i, j] = dp[i, j - 1];
}
}
}
return dp[dp.GetUpperBound(0), dp.GetUpperBound(1)];
}
}
No developer has claimed the Go throne for LeetCode #392 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
t[i] and s[j].t, i is automatically incremented by 1, and if t[i] == s[j], then j += 1.j >= s.length, return true. If t is traversed and still not returned in advance, return false.func isSubsequence(s string, t string) bool {
if len(s) == 0 {
return true
}
sIndex := 0
for i := 0; i < len(t); i++ {
if t[i] == s[sIndex] {
sIndex++
if sIndex >= len(s) {
return true
}
}
}
return false
}
Solution 1 is essentially a "dynamic programming" algorithm implemented with rolling variables. It is easy to understand, and the space complexity is reduced to O(1).Because it can handle a more complex scenario (e.g. 583. Delete Operation for Two Strings) that is common in dynamic programming.
one-dimensional rolling array to implement the "dynamic programming" algorithm?
Of course, but considering that for this problem a one-dimensional rolling array is not as easy to understand as a two-dimensional array, and the implementation process is also prone to errors, so here I didn't give the relevant code implementation. If you are interested, you can try it.
two-dimensional arrays for dynamic programming.func isSubsequence(s string, t string) bool {
dp := make([][]bool, len(s) + 1)
columnSize := len(t) + 1
dp[0] = slices.Repeat([]bool{true}, columnSize)
for i := 1; i < len(dp); i++ {
dp[i] = make([]bool, columnSize)
}
for i := 1; i < len(dp); i++ {
for j := 1; j < len(dp[0]); j++ {
if s[i - 1] == t[j - 1] {
dp[i][j] = dp[i - 1][j - 1]
} else {
dp[i][j] = dp[i][j - 1]
}
}
}
return dp[len(dp) - 1][len(dp[0]) - 1]
}
No developer has claimed the Javascript throne for LeetCode #392 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
t[i] and s[j].t, i is automatically incremented by 1, and if t[i] == s[j], then j += 1.j >= s.length, return true. If t is traversed and still not returned in advance, return false./**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isSubsequence = function(s, t) {
if (s.length === 0) {
return true;
}
let sIndex = 0;
for (let i = 0; i < t.length; i++) {
if (t[i] === s[sIndex]) {
sIndex++;
if (sIndex >= s.length) {
return true;
}
}
}
return false;
};
Solution 1 is essentially a "dynamic programming" algorithm implemented with rolling variables. It is easy to understand, and the space complexity is reduced to O(1).Because it can handle a more complex scenario (e.g. 583. Delete Operation for Two Strings) that is common in dynamic programming.
one-dimensional rolling array to implement the "dynamic programming" algorithm?
Of course, but considering that for this problem a one-dimensional rolling array is not as easy to understand as a two-dimensional array, and the implementation process is also prone to errors, so here I didn't give the relevant code implementation. If you are interested, you can try it.
two-dimensional arrays for dynamic programming.var isSubsequence = function (s, t) {
const dp = Array(s.length + 1).fill().map(
() => Array(t.length + 1).fill(false)
)
dp[0].fill(true)
for (let i = 1; i < dp.length; i++) {
for (let j = 1; j < dp[0].length; j++) {
if (s[i - 1] == t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1]
} else {
dp[i][j] = dp[i][j - 1]
}
}
}
return dp.at(-1).at(-1)
};
No developer has claimed the Ruby throne for LeetCode #392 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
t[i] and s[j].t, i is automatically incremented by 1, and if t[i] == s[j], then j += 1.j >= s.length, return true. If t is traversed and still not returned in advance, return false.# @param {String} s
# @param {String} t
# @return {Boolean}
def is_subsequence(s, t)
return true if s.empty?
s_index = 0
t.each_char.with_index do |char, i|
if char == s[s_index]
s_index += 1
return true if s_index >= s.length
end
end
false
end
Solution 1 is essentially a "dynamic programming" algorithm implemented with rolling variables. It is easy to understand, and the space complexity is reduced to O(1).Because it can handle a more complex scenario (e.g. 583. Delete Operation for Two Strings) that is common in dynamic programming.
one-dimensional rolling array to implement the "dynamic programming" algorithm?
Of course, but considering that for this problem a one-dimensional rolling array is not as easy to understand as a two-dimensional array, and the implementation process is also prone to errors, so here I didn't give the relevant code implementation. If you are interested, you can try it.
two-dimensional arrays for dynamic programming.def is_subsequence(s, t)
dp = Array.new(s.size + 1) do |i|
Array.new(t.size + 1, i == 0 ? true : false)
end
(1...dp.size).each do |i|
(1...dp[0].size).each do |j|
dp[i][j] =
if s[i - 1] == t[j - 1]
dp[i - 1][j - 1]
else
dp[i][j - 1]
end
end
end
dp[-1][-1]
end
No developer has claimed the Rust throne for LeetCode #392 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #392 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #392 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.