Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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).
Example 1:
Input: s = "abc", t = "ahbgdc"
Output: true
Example 2:
Input: s = "axc", t = "ahbgdc"
Output: false
Constraints:
0 <= s.length <= 100
0 <= t.length <= 104
s
and t
consist only of lowercase English letters.Follow up: Suppose there are lots of incoming s
, say s1, s2, ..., sk
where k >= 109
, and you want to check one by one to see if t
has its subsequence. In this scenario, how would you change your code?
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if len(s) > len(t):return False
if len(s) == 0:return True
subsequence=0
for i in range(0,len(t)):
if subsequence <= len(s) -1:
print(s[subsequence])
if s[subsequence]==t[i]:
subsequence+=1
return subsequence == len(s)
class Solution {
public:
bool isSubsequence(string s, string t) {
int n = s.length(),m=t.length();
int j = 0;
// For index of s (or subsequence
// Traverse s and t, and
// compare current character
// of s with first unmatched char
// of t, if matched
// then move ahead in s
for (int i = 0; i < m and j < n; i++)
if (s[j] == t[i])
j++;
// If all characters of s were found in t
return (j == n);
}
};
public class Solution {
public boolean isSubsequence(String s, String t) {
if (s.length() == 0) return true;
int indexS = 0, indexT = 0;
while (indexT < t.length()) {
if (t.charAt(indexT) == s.charAt(indexS)) {
indexS++;
if (indexS == s.length()) return true;
}
indexT++;
}
return false;
}
}
In our experience, we suggest you solve this Is Subsequence LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Is Subsequence LeetCode Solution
I hope this Is Subsequence LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>