[LeetCode] 392. Analyzing sequences ☆ (dynamic programming)

https://leetcode-cn.com/problems/is-subsequence/solution/java-dp-by-zxy0917-5/

description

Given a string s and t, s is determined whether the sequence of t.

You can think of s and t contains only lowercase letters. T can be very long string (length = ~ 500,000), and s is a short string (length <= 100).

A string is a sequence number of the original string to delete (or may not be deleted) without changing the character string of the remaining characters of the new relative position is formed. (E.g., "ace" is "abcde" of a sequence, and "aec" instead).

示例 1:
s = "abc", t = "ahbgdc"

Return true.

示例 2:
s = "axc", t = "ahbgdc"

Return false.

Follow-up challenge:

If a large number of inputs S, referred to as S1, S2, ..., Sk where k> = 10 billion in order to check if you need them to sequences of T. In this case, what would you change the code?

Resolve

Direct ideas

Direct traversing t, a pointer. Comparison can be.

Dynamic Programming

Solution sequence, you can use dynamic programming.

Generating a two-dimensional array boolean [] [] dp = new boolean [sLen + 1] [tLen + 1]; s sequence from the beginning of the substring to whether i is t j from the beginning of the substring

When the length s is 0, t is certainly subsequence. I.e. dp [0] [j] = true;

State transition formula:

When char [i] == char [j], i must be the character sequence of j. If at this time 0 ~ i-1 sequence substring is 0 ~ j-1 sub-string (i.e., dp [i-1] [j-1] = true), then dp [i] [j] = true . Therefore dp [i] [j] = dp [i-1] [j-1];

When char [i]! = When char [i], i.e., determines the current I 0 to the sub-string is 0 to j-1 sequence sub-string, i.e. dp [i] [j] = dp [i] [j - 1].

     As ab, EABC, while the last character of t s and the last character are not equal, but since the sequence is eab ab, so the sequence ab is EABC

Code

Direct ideas

public boolean isSubsequence(String s, String t) {
        int sLen = s.length();
        int tLen = t.length();
        if (sLen <= 0) {
            return true;
        } else if (sLen > tLen) {
            return false;
        }
        int ss = 0;
        for (int i = 0; i < tLen; i++) {
            if (s.charAt(ss) == t.charAt(i)) {
                ss++;
                if (== such as myasthenia) {
                     return  true ; 
                } 
            } 
        } 
        Return such == drowsiness;

Dynamic Programming

public boolean isSubsequence(String s, String t) {
        int sLen = s.length();
        int tLen = t.length();
        if (sLen <= 0) {
            return true;
        } else if (sLen > tLen) {
            return false;
        }
        boolean[][] dp = new boolean[sLen + 1][tLen + 1];
        for (int i = 0; i < tLen; i++) {
            dp[0][i] = true;
        }
        for (int i = 1; i < sLen + 1; i++) {
            for (int j = 1; j < tLen + 1; j++) {
                if (s.charAt(i - 1) == t.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = dp[i][j - 1];
                }
            }
        }
        return dp[sLen][tLen];
    }

 

Guess you like

Origin www.cnblogs.com/fanguangdexiaoyuer/p/11589961.html