LeetCode 392. 判断子序列(C、python)

给定字符串 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.

C

bool isSubsequence(char* s, char* t) 
{
    int m=strlen(s);
    int n=strlen(t);
    int a1=0;
    int a2=0;
    while(a1<m && a2<n)
    {
        if(s[a1]==t[a2])
        {
            a1++;
        }
        a2++;
    }
    if(m==a1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

python

class Solution:
    def isSubsequence(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        m=len(s)
        n=len(t)
        a1=0
        a2=0
        while a1<m and a2<n:
            if s[a1]==t[a2]:
                a1+=1
            a2+=1
        if m==a1:
            return True
        else:
            return False
        

猜你喜欢

转载自blog.csdn.net/qq_27060423/article/details/84680521