题解 | #特工的密码#

特工的密码

https://www.nowcoder.com/practice/bcdfed09de534aea92b24c73699dba5c

知识点

字符串

解题思路

将point指针指向s的头部,遍历t,如果当前字符与s的point位置相同,point就加一,当point走完了s说明t中存在s,返回true。走完t,point也没有到终点说明s不是s的子序列。

Java题解

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @param t string字符串 
     * @return bool布尔型
     */
    public boolean isSubsequence (String s, String t) {
        // write code here
        int n = s.length(), m = t.length();
        int point = 0;
        for(int i = 0; i < m; i++){
            if(t.charAt(i) == s.charAt(point)){
                point++;
                if(point == n){
                    return true;
                }
            }
        }
        return false;
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务