LeetCode 392. Is Subsequence(判断子序列)

s = "abc", t = "ahbgdc"
Return true.

不可改变字符的相对位置
  public boolean isSubsequence(String s, String t) {
        //双指针法
        int si = 0, ti = 0;
        char[] snew = s.toCharArray(), tnew = t.toCharArray();

        while(si < s.length() && ti < t.length()) {
            if(snew[si] == tnew[ti]) {
                si ++;
                ti ++;
            }
            else {
                ti ++;
            }
        }
        return si == s.length();
    }
发布了581 篇原创文章 · 获赞 97 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/gx17864373822/article/details/104900615
今日推荐