LeetCode #392: Is Subsequence
Problem
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
.
Solution
題意
給出兩個字串s和t,要求判斷s是否是t的子串。
該題中對於子串的定義是:只要s中的字母以相同的順序出現在t中,那麼我們就說s是t的子串
示例見原題。
分析
從貪心演算法的角度去看,解決這道題的思路是每判斷一次都得到一個目前看起來最優的解,那麼對於這道題來說,就要求每判斷一次都能將問題的規模縮小。
所以解法就是。從s的第一個字母s[0]開始,從t[0]開始逐個匹配t中的字母,如果在t[i]處匹配,則繼續匹配s[1],從t[i+1]開始。
重複這個過程,直到s中的字母全部完成匹配,則返回true
;否則,到達t[t.size()-1]時仍未完成s的匹配,返回false
。
Code
//Runtime: 68ms
class Solution {
public:
bool isSubsequence(string s, string t) {
int sIndex = 0, tIndex = 0; //index of string s and string t
int sSize = s.size(), tSize = t.size();
if (sSize == 0) return true; //if s is empty string, s is every string's sub-string
while (tIndex < tSize) { //check all characters of string t
if (s[sIndex] == t[tIndex]) {
sIndex++; //if s[sIndex] and t[tIndex] are matched, then check the next character of s
if (sIndex == sSize) //which means all characters of s are matched
return true;
}
tIndex++;
}
return false;
}
};
相關文章
- leetcode392. Is SubsequenceLeetCode
- Leetcode: Arithmetic Slices II - SubsequenceLeetCode
- LeetCode-392-判斷子序列LeetCode
- LeetCode-Wiggle SubsequenceLeetCode
- LeetCode-Longest Increasing SubsequenceLeetCode
- LeetCode-Increasing Triplet SubsequenceLeetCode
- [LeetCode] 727. Minimum Window SubsequenceLeetCode
- 【LeetCode】Increasing Triplet Subsequence(334)LeetCode
- [LeetCode] 674. Longest Continuous Increasing SubsequenceLeetCode
- 【Leetcode】1081. Smallest Subsequence of Distinct CharactersLeetCode
- 【Leetcode】1673. Find the Most Competitive SubsequenceLeetCode
- LeetCode C++ 376. Wiggle Subsequence【Dynamic Programming】中等LeetCodeC++
- Missing Subsequence Sum
- [LeetCode] Is Subsequence 判斷一個字串是否為另一個的子序列LeetCode字串
- Algorithm for Maximum Subsequence Sum zGo
- [atcoder 349] [F - Subsequence LCM]
- B. Missing Subsequence Sum
- Lintcode 1263. Is Subsequence
- LintCode-Longest Increasing Subsequence
- LintCode-Longest Common Subsequence
- Oracle 11G OCP 1Z0-053 392Oracle
- CF163A Substring and Subsequence 題解
- [ARC186E] Missing Subsequence 題解
- HDU 3530 Subsequence (dp+單調佇列)佇列
- itm tep無法登陸問題(錯誤號:KFWITM392E)
- 【Lintcode】398. Longest Continuous Increasing Subsequence II
- [LintCode] Longest Increasing Subsequence 最長遞增子序列
- 「暑期訓練」「基礎DP」 Common Subsequence (POJ-1458)
- [題解]AT_abc299_f [ABC299F] Square Subsequence
- Codeforces 163A Substring and Subsequence:dp【子串與子序列匹配】
- HDU4991 Ordered Subsequence (dp+樹狀陣列+離散化)陣列
- 您的業務遭遇資料洩露?預計會有392萬美元的賬單
- IFR:2022年每1萬名中國員工對應392臺機器人機器人
- 【LeetCode】如何學習LeetCode?LeetCode
- LeetcodeLeetCode
- 2020ICPC·小米 網路選拔賽第二場 Subsequence Pair(貪心二分)AI
- LeetCode in actionLeetCode
- Leetcode AnagramsLeetCode