Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit series activities – click on the task to see the details of the activities

describe

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
Copy the code

Note:

0 <= s.length <= 100
0 <= t.length <= 10^4
s and t consist only of lowercase English letters.
Copy the code

parsing

Given two strings s and t, return true if S is a subsequence of t, false otherwise.

A subsequence of strings is a new string formed from the original string by deleting some characters without interfering with the relative positions of the remaining characters. (that is, “ace” is a subsequence of “ABCDE” and “AEC” is not).

The length of s is 100, the length of t is 10000, and the length of s is 100. The length of t is 10000. We can directly traverse every character from left to right in base S, and we also traverse every character in T from left to right. We return True if we can find an equal character in the same relative position, otherwise we return False.

We could have used Python’s built-in index function to find the index position, which would have made the code a little easier.

The time complexity is O(s+t) and the space complexity is O(1).

answer

class Solution(object): def isSubsequence(self, s, t): """ :type s: str :type t: str :rtype: bool """ s_i = t_i = 0 while s_i < len(s): while t_i < len(t) and s[s_i]! =t[t_i]: t_i += 1 if t_i >= len(t): return False s_i += 1 t_i += 1 return TrueCopy the code

The results

Given the given list in the Python online submission. Memory Usage: 10 ms Given in the Python online submissions for Is Subsequence.Copy the code

The original link

Leetcode.com/problems/is…

Your support is my biggest motivation