Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

392. Is Subsequence

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:

1
2
Input: s = "abc", t = "ahbgdc"
Output: true

Example 2:

1
2
Input: s = "axc", t = "ahbgdc"
Output: false
1
2
3
4
5
6
7
8
9
10
TC: O(n) SC:O(1)
class Solution {
public boolean isSubsequence(String s, String t) {
int ls = s.length(), lt = t.length(), i = 0;
if(ls>lt) return false;
for(int j = 0;j<lt && i<ls;j++)
if(t.charAt(j) == s.charAt(i)) i++;
return i == s.length();
}
}

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels