首页 > 试题广场 >

判断子序列

[编程题]判断子序列
  • 热度指数:2833 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定两个字符串 S 和 T ,判断 S 是否是 T 的子序列。
即是否可以从 T 删除一些字符转换成 S。

数据范围: ,保证字符串中仅含有小写字母
示例1

输入

"nowcoder","nowcoder"

输出

true
示例2

输入

"nower","nowcoder"

输出

true
示例3

输入

"nowef","nowcoder"

输出

false
struct Solution{

}

impl Solution {
    fn new() -> Self {
        Solution{}
    }

    /**
    * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    *
    * 
        * @param S string字符串 
        * @param T string字符串 
        * @return bool布尔型
    */
    pub fn isSubsequence(&self, S: String, T: String) -> bool {
        // write code here
        self.check(&S[..], &T[..])
    }

    fn check(&self, s: &str, t: &str) -> bool {
        if s == "" && t == "" {return true}
        if s == "" {return false}
        if t == "" {return false}
        if s[0 ..= 0] == t[0 ..= 0] {
            self.check(&s[1 ..], &t[1 ..])
        } else {
            self.check(s, &t[1 ..])
        }
    }
}

发表于 2023-09-19 21:50:35 回复(0)

问题信息

难度:
1条回答 3614浏览

热门推荐

通过挑战的用户

查看代码