3
头图

Judgment subsequence

Topic description: Given strings s and t, determine whether s is a subsequence of t.

A subsequence of a string is a new string formed by removing some (or not) characters from the original string without changing the relative positions of the remaining characters. (For example, "ace" is a subsequence of "abcde", but "aec" is not).

Advanced:

If you have a large number of inputs S, called S1, S2, ... , Sk where k >= 1 billion, you need to check in turn whether they are subsequences of T. In this case, how would you change the code?

For example descriptions, please refer to the official website of LeetCode.

Source: LeetCode
Link: https://leetcode-cn.com/problems/is-subsequence/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

Solution 1: Double pointer traversal

First, judge several special scenarios:

  • If the substring of s is empty, then s must be a subsequence of t, and return true directly;
  • If s is not empty and t is empty, then s cannot be a subsequence of t, and returns false directly;
  • If the length of the substring of s is greater than the length of t, then s cannot be a substring of t, and returns false directly.

If it is not a special case, use double pointers to point to the first characters of s and t respectively, and then traverse the characters of s and t. The traversal process is as follows:

  • If the length of the string that s has not yet traversed is greater than the length of the string that t has not yet traversed, then s cannot be a substring of t, and returns false directly;
  • If the characters at the current position of s and t are the same, the pointer moves one bit backward at the same time;
  • If the characters at the current position of s and t are the same, the pointer to t is moved one bit backward;
  • The condition for the end of the traversal is that s or t traverse to the last bit.

Finally, it is judged that if the traversal of s is completed, it means that s is a subsequence of t, and returns true; otherwise, returns false.

 public class LeetCode_392 {
    /**
     * 双指针
     *
     * @param s
     * @param t
     * @return
     */
    public static boolean isSubsequence(String s, String t) {
        /**
         * 如果s子串为空,则s一定是t的子序列,直接返回true
         */
        if (s == null || s.length() == 0) {
            return true;
        }
        // 如果s不为空,t为空,则s不可能是t的子序列,直接返回false
        if (t == null || t.length() == 0) {
            return false;
        }
        // 如果s子串的长度大于t的长度,则s不可能是t的子串,直接返回false
        if (s.length() > t.length()) {
            return false;
        }
        // 分别指向s和t的第一个字符
        int sIndex = 0, tIndex = 0;
        // 一个个的遍历字符,直到遍历到s或t的最后一个字符
        while (sIndex < s.length() && tIndex < t.length()) {
            // 如果s还未遍历的字符串长度大于t还未遍历的字符串长度,则s不可能是t的子串,直接返回false
            if (s.length() - sIndex > t.length() - tIndex) {
                return false;
            }
            // 如果s和t当前位置的字符相同,则指针同时往后移动一位
            if (s.charAt(sIndex) == t.charAt(tIndex)) {
                sIndex++;
                tIndex++;
            } else {
                // 如果s和t当前位置的字符相同,则指向t的指针往后移动一位
                tIndex++;
            }
        }
        // 最后,如果s遍历完成,说明s是t的子序列,返回true
        if (sIndex == s.length()) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        // 测试用例,期望返回: true
        System.out.println(isSubsequence("abc", "ahbgdc"));
    }
}
[Daily Message] Stand up and be a man, bend down and do things.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!