国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

392. Is Subsequence -Medium

2019-11-14 12:44:03
字體:
來源:轉載
供稿:網友

Question

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).

給出兩個字符串s, t,判斷s是否是t的子集。t大約有500000個字符, s不超過100個字符。這里的子集的判斷是允許刪除一些中間的字符得到的剩余字符相同(也就是不需要連續相同的字符串)

Example

s = “abc”, t = “ahbgdc”

Return true.


s = “axc”, t = “ahbgdc”

Return false.

Solution

這道題還是比較簡單的,如果s是t的子集,那么s中的所有字符必然按照相同的順序出現在t中,所以我們可以分別建立兩個字符串的索引,每當t中出現s的當前字符,就搜索s的下一個字符,直到s的所有字符均在t中找到

class Solution(object): def isSubsequence(self, s, t): """ :type s: str :type t: str :rtype: bool """ # 如果s是空字符串,那么它是任意字符串的子集 if len(s) == 0: return True index_s, index_t = 0, 0 # 遍歷t for current_t in t: # 如果s的當前元素被找到,則繼續尋找下一個s if s[index_s] == current_t: index_s += 1 # 如果s的所有字符都被找到,代表s是t的子集 if index_s == len(s): return True # 如果搜索完t仍然沒有找全s,代表s不是t的子集 return False
上一篇:vs2012轉成vs2010

下一篇:java中的異常分類

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 织金县| 嘉定区| 哈密市| 黄大仙区| 冕宁县| 乌兰浩特市| 饶平县| 滨州市| 马关县| 伊宁市| 定日县| 隆化县| 耿马| 竹溪县| 娄底市| 白朗县| 鄂州市| 石台县| 宜川县| 大厂| 磴口县| 南充市| 乐平市| 荆州市| 民权县| 高尔夫| 都安| 通山县| 剑河县| 兰西县| 菏泽市| 兴城市| 津南区| 岚皋县| 濮阳市| 堆龙德庆县| 新干县| 平利县| 儋州市| 瑞金市| 铁力市|