(Java) LeetCode 392. Is Subsequence —— 判斷子序列

來源:互聯網
上載者:User

標籤:遍曆   let   for   問題   直接   一個   字串   tin   which   

Given a string s and a string t, check if s is subsequence of t.

You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and sis a short string (<=100).

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).

Example 1:
s = "abc", t = "ahbgdc"

Return true.

Example 2:
s = "axc", t = "ahbgdc"

Return false.

Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?

 

 

解法一(貪心演算法):其實有點像字串匹配的問題。如果維持兩個指標分別指向兩個字串起點並開始遍曆,找到匹配就移動兩個指標向後,找到不匹配只移動待搜尋的字串的指標以尋找下一個匹配點。最後判斷時候目標字串已被遍曆完成即可。思路很簡單,而且是有一種“貪心”的想法在裡面,即遇到匹配的就加入。

 

解法二(動態規劃):看到follow up和標籤,想想其他的辦法。首先動態規劃是一定可以做的。假設boolean dp[i][j]表示s從0到i位置的字串是否是t從0到j位置字串的子串。那麼當s[i]和t[j]相等的時候,只需要判斷s(0, i-1)是不是t(0, j-1)的子串;如果s[i]和t[j]不相等,那麼要一直往前看s(0, i)是不是t(0, j-1)的子串。只要注意邊界情況就好。如果j是0,即t只有一個字元的時候都還沒有能和s匹配,那麼一定是不匹配。而且如果i>j,那麼dp[i][j]一定是false,因為短字串沒法匹配長字串。把遞推公式實現,就是動態規劃的解法。雖然運行速度很慢,但還是能過,顯然這個方法是被允許的。

 

解法三(二分尋找):最後想一下,如果輸入是大量的s需要進匹配那要怎麼做。s可以有很多,但t只有一個。如果把t裡面每個字元出現時的索引存起來,這樣遇到一個s,那就看首先s中的字元在t中有沒有出現(即儲存那個索引的結構是不是空),沒有出現直接返回false。如果出現了,那麼記下它在t中出現的位置,比如設為preIndex,之後搜尋s中下一個字元。這時候如果字元仍然在t中存在,那其實應該尋找第一個大於preIndex的位置(即尋找第一個大於等於preIndex+1的索引)才可以實現匹配。在有序結構裡尋找特定數,就是標籤中有二分尋找的由來。

 

解法一(Java)

public class Solution {    public boolean isSubsequence(String s, String t) {        int i = 0, j = 0;        while(i < s.length() && j < t.length()) {            if(s.charAt(i) == t.charAt(j)) i++;            j++;        }        if(i == s.length()) return true;        return false;    }}

 

解法二(Java)

public class Solution {    public boolean isSubsequence(String s, String t) {        int m = s.length(), n = t.length();        if (m == 0) return true;        if (n == 0) return false;        boolean[][] dp = new boolean[m][n];        for (int i = 0; i < m; i++) {            for (int j = 0; j < n; j++) {                if (i > j) dp[i][j] = false;                else if (s.charAt(i) == t.charAt(j)) {                    if (i == 0 || j == 0) dp[i][j] = i <= j ? true : false;                    else           dp[i][j] = dp[i-1][j-1];                }                 else dp[i][j] = j == 0 ? false : dp[i][j-1];            }        }            return dp[m-1][n-1];    }   }

 

解法三(Java)

public class solution {    public boolean isSubsequence(String s, String t) {        int m = s.length(), n = t.length();        if (m == 0) return true;        if (n == 0) return false;        List<Integer>[] list = new List[256];        for (int i = 0; i < n; i++) {            char c = t.charAt(i);            if (list[c] == null) list[c] = new ArrayList<Integer>();            list[c].add(i);        }        int preIndex = -1;        for (int i = 0; i < m; i++) {            char c = s.charAt(i);            if (list[c] == null) return false;            int index = binarySearch(list[c], preIndex + 1);            if (index == -1) return false;            else preIndex = index;        }        return true;
}
public int binarySearch(List<Integer> list, int key) { int lo = 0, hi = list.size() - 1; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (key > list.get(mid)) lo = mid + 1; else hi = mid; } if (list.get(hi) < key) return -1; return list.get(hi);
}}

 

(Java) LeetCode 392. Is Subsequence —— 判斷子序列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.