LeetCode | 0392. Is Subsequence判断子序列【Python】

LeetCode 0392. Is Subsequence判断子序列【Easy】【Python】【双指针】

Problem

LeetCode

Given a string s and a string t, check if s is subsequence of t.

You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).

Example 1:
s = "abc", t = "ahbgdc"

Return true.

Example 2:
s = "axc", t = "ahbgdc"

Return false.

Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?

Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.

问题

力扣

给定字符串 s 和 t ,判断 s 是否为 t 的子序列。

你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=100)。

字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。

示例 1:
s = "abc", t = "ahbgdc"

返回 true.

示例 2:
s = "axc", t = "ahbgdc"

返回 false.

后续挑战 :

如果有大量输入的 S,称作S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?

致谢:

特别感谢 @pbrother 添加此问题并且创建所有测试用例。

思路

双指针

i 指针作为 s 的索引,j 指针作为 t 的索引。每次 j 都要右移,如果 s[i] == t[j],i 要右移。

时间复杂度: O(min(len(s), len(t)))
空间复杂度: O(1)

Python代码

class Solution(object):
    def isSubsequence(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        i, j = 0, 0
        while i < len(s) and j < len(t):
            if s[i] == t[j]:
                i += 1
            j += 1
        return i == len(s)

代码地址

GitHub链接

原文地址:https://www.cnblogs.com/wonz/p/12335210.html

时间: 2024-11-05 18:46:38

LeetCode | 0392. Is Subsequence判断子序列【Python】的相关文章

(Java) LeetCode 392. Is Subsequence —— 判断子序列

Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and sis a short string (<=100). A subsequence of a

LeetCode 376. Wiggle Subsequence 摆动子序列

原题 A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than t

LeetCode:递增的三元子序列【334】

LeetCode:递增的三元子序列[334] 题目描述 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false . 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1) . 示例 1: 输入: [1,2,3,4,5] 输出: true 示例 2: 输入:

leetcode 【 Pascal&#39;s Triangle 】python 实现

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 代码:oj测试通过 Runtime: 46 ms 1 class Solution: 2 # @return a list of lists of integers 3 def generat

[leetcode]Binary Tree Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal/ 题意:二叉树的层序遍历的实现. 解题思路:二叉树的层序遍历可以用bfs或者dfs来实现.这里使用的dfs实现,代码比较简洁.实际上,二叉树的先序遍历就是dfs实现.   比如一棵树如下: 1 /  \ 2       3 /    \    /   \ 4     5  6    7    二叉树的先序遍历为{1,2,4,5,3,6,7},可以看到这个遍

最长公共子序列python实现

最长公共子序列是动态规划基本题目,下面按照动态规划基本步骤解出来. 1.找出最优解的性质,并刻划其结构特征 序列a共有m个元素,序列b共有n个元素,如果a[m-1]==b[n-1],那么a[:m]和b[:n]的最长公共子序列长度就是a[:m-1]和b[:n-1]的最长公共子序列长度+1:如果a[m-1]!=b[n-1],那么a[:m]和b[:n]的最长公共子序列长度就是MAX(a[:m-1]和b[:n]的最长公共子序列长度,a[:m]和b[:n-1]的最长公共子序列长度). 2.递归定义最优值

LeetCode OJ - Distinct Subsequence

这道题采用动态规划,可是我一开始没有想到.后来参考了discuss中前辈的代码和思路,才想通的. 方法二是因为每一步只和上一步的内容相关,所以可以只用O(n)的空间复杂度. 下面是AC代码: 1 /** 2 * Solution DP 3 * we keep a m*n matrix and scanning through string T, 4 * p[i][j] means the number of distinct subsequence of S(0...j) equal to T(

[leetcode]Search in Rotated Sorted Array @ Python

原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array/ 题意: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in

[leetcode]Unique Binary Search Trees II @ Python

原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上一题要求的是符合条件的二叉查找树的棵数,我们上一题提过,求个数一般思路是动态规划,而枚举的话,我们就考虑dfs了.dfs(start, end)函数返回以start,start+1,...,end为根的二叉查找树. 代码: # Definition for a binary tree node #