| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
 本帖最后由 Judie 于 2023-5-23 20:23 编辑  
 
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 
 
Example 2: 
Input: s = "axc", t = "ahbgdc" 
Output: false 
  
Constraints: 
0 <= s.length <= 100 
0 <= t.length <= 104 
s and t consist only of lowercase English letters. 
  
Follow up:  
Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 109, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code? 
 
- class Solution(object):
 
 -     def isSubsequence(self, s, t):
 
 -         """
 
 -         :type s: str
 
 -         :type t: str
 
 -         :rtype: bool
 
 -         """
 
  复制代码 
 
Judy 
python 
- class Solution(object):
 
 -     def isSubsequence(self, s, t):
 
 -         """
 
 -         :type s: str
 
 -         :type t: str
 
 -         :rtype: bool
 
 -         """
 
  
-         j = 0
 
 -         for x in s:
 
 -             idx = t[j:].find(x)
 
 -             if idx == -1:
 
 -                 return False
 
 -             j += idx + 1  
 
 -         
 
 -         return True
 
  复制代码 
 
Gray 
c++ better version?! 
- class Solution {
 
 - public:
 
 -     bool isSubsequence(string s, string t) {
 
 -         if(s == "") return true;
 
 -         if(t == "") return false;
 
 -         int index = 0;
 
 -         for(int i = 0; i < t.size(); i++){
 
 -            if(s[index]==t[i]){
 
 -                index++;
 
 -            }
 
 -            if(index==s.size()){
 
 -                return true;
 
 -            }
 
 -         }
 
 -         return false;
 
 -     }
 
 - };
 
  复制代码 
 
 |   
 
 
 
 |