编程语言
首页 > 编程语言> > 天池 在线编程 能否转换

天池 在线编程 能否转换

作者:互联网

文章目录

1. 题目

给两个字符串 S 和 T, 判断 S 能不能通过删除一些字母(包括0个)变成 T.

样例1
输入: S = "lintcode" 和 T = "lint"
输出: true

样例2
输入: S = "lintcode" 和 T = "ide"
输出: true

样例3
输入: S = "adda" and T = "aad"
输出: false
解释: 无论如何,你都不能通过删除一个'd' 把 "adda" 变成 "aad"。

https://tianchi.aliyun.com/oj/286614371019772507/338469151696950135

2. 解题

class Solution {
public:
    /**
     * @param s: string S
     * @param t: string T
     * @return: whether S can convert to T
     */
    bool canConvert(string &s, string &t) {
        // Write your code here
        int n1 = s.size(), n2 = t.size();
        int i = 0, j = 0;
        while(i < n1 && j < n2)
        {
            if(s[i] == t[j])
                i++,j++;
            else
                i++;
        }
        return j==n2;
    }
};

8ms C++


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

标签:return,在线,++,编程,样例,int,天池,n2,string
来源: https://blog.csdn.net/qq_21201267/article/details/114181678