?LeetCode刷題實(shí)戰(zhàn)392:判斷子序列
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).
示例
示例 1:
輸入:s = "abc", t = "ahbgdc"
輸出:true
示例 2:
輸入:s = "axc", t = "ahbgdc"
輸出:false
解題
class Solution {
public boolean isSubsequence(String s, String t) {
int n = s.length(), m = t.length();
int i = 0, j = 0; //i -> s , j -> t
while(i < n && j < m){
if(s.charAt(i) == t.charAt(j)) i++; // 相等時(shí),i++
j++; // 不管是否相等,j++
}
return i == n ;
}
}
LeetCode1-380題匯總,希望對(duì)你有點(diǎn)幫助!
LeetCode刷題實(shí)戰(zhàn)381:O(1) 時(shí)間插入、刪除和獲取隨機(jī)元素
LeetCode刷題實(shí)戰(zhàn)382:鏈表隨機(jī)節(jié)點(diǎn)
LeetCode刷題實(shí)戰(zhàn)383:贖金信
LeetCode刷題實(shí)戰(zhàn)384:打亂數(shù)組
LeetCode刷題實(shí)戰(zhàn)385:迷你語(yǔ)法分析器
