1. 程式人生 > >LeetCode | Interleaving String(交叉字串)

LeetCode | Interleaving String(交叉字串)

Given s1s2s3, find whether s3 is formed by the interleaving of s1 and s2.

For example,
Given:
s1 = "aabcc",
s2 = "dbbca",

When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.


題目解析:

看s1和s2正序交替排列是否能得到目標串。

方案一:遞迴方法

由於兩個子串是從前向後與目標串匹配的,那麼可以一個一個去比較,當s1[index1] == s2[index2]的時候,就進行遞迴:先按照s1當前值與目標串s3相等,遞迴;如果不能匹配,則按照s2與s3當前值相等,遞迴;最後再進行一次判斷。

由於遞迴耗時,造成時間超限……

class Solution {
public:
    bool isInterleave(string s1, string s2, string s3) {
        int len1 = s1.size();
        int len2 = s2.size();
        int len3 = s3.size();

        if(len1+len2 != len3)
            return false;
        return Judge(s1,0,s2,0,s3,0);
    }
    bool Judge(string s1,int index1,string s2,int index2,string s3,int index3){
        if(index3 == s3.size())
            return true;
        if(index1 == s1.size()){
            while(index2 < s2.size()){
                if(s2[index2] != s3[index3])
                    return false;
                index2++;
                index3++;
            }
            return true;
        }
        if(index2 == s2.size()){
            while(index1 < s1.size()){
                if(s1[index1] != s3[index3])
                    return false;
                index1++;
                index3++;
            }
            return true;
        }
        bool flag1 = false,flag2 = false;
        if(s1[index1] == s3[index3])    //如果兩個相等,就遞迴進去
            flag1 = Judge(s1,index1+1,s2,index2,s3,index3+1);

        if(flag1 == false){ //如果遞迴失敗,或者s1[index1] != s3[index3]
            if(s2[index2] == s3[index3])
                flag2 = Judge(s1,index1,s2,index2+1,s3,index3+1);
            else
                return false;
        }

        return flag1 || flag2;
    }
};


方案二:

時間超限問題,我們可以利用動態規劃來解決。假設0...len1和0....len2已經於0...len3進行匹配了,然後增加一個值len3+1,那麼要麼滿足0...len1+1且0....len2  要麼滿足0...len1和0....len2+1。基於這個思想,就定義一個二維陣列。填寫動態規劃產生的中間值,以空間換取時間上的節省。--------因此,空間換時間不光用在提高時間複雜度上面,也用在動態規劃中!要有這個思想。

class Solution {
private:
    bool f[1000][1000];
public:
    bool isInterleave(string s1, string s2, string s3) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function 
        if (s1.size() + s2.size() != s3.size())
            return false;
            
        f[0][0] = true;
        for(int i = 1; i <= s1.size(); i++)
            f[i][0] = f[i-1][0] && (s3[i-1] == s1[i-1]);
            
        for(int j = 1; j <= s2.size(); j++)
            f[0][j] = f[0][j-1] && (s3[j-1] == s2[j-1]);
            
        for(int i = 1; i <= s1.size(); i++)
            for(int j = 1; j <= s2.size(); j++)
                f[i][j] = (f[i][j-1] && s2[j-1] == s3[i+j-1]) || (f[i-1][j] && s1[i-1] == s3[i+j-1]);
                
        return f[s1.size()][s2.size()];
    }
};

相關推薦

LeetCode | Interleaving String交叉字串

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given:s1 = "aabcc",s2 = "dbbca", When s3 

394. Decode String 解碼字串

Given an encoded string, return it’s decoded string. The encoding rule is: k[encoded_string], where the encoded_strin

leetcode 97. Interleaving String字串交錯出現 DFS深度優先遍歷 + 很明顯很經典的DP動態規劃做法

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = “aabcc”, s2 = “dbbca”,

LeetCode】345.Reverse Vowels of a String反轉字串中的母音字母-C++實現

本題為谷歌面試題。 問題描述: 一、第一種方法:對撞指標法 #include <iostream> #include <vector> #include <string> #include <cassert> #inc

LeetCode-面試演算法經典-Java實現】【151-Reverse Words in a String反轉字串中的單詞

原題   Given an input string, reverse the string word by word.   For example,   Given s = "the sky is blue",   return "bl

LeetCode 438. Find All Anagrams in a String找到字串中所有字母異位詞

Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s. Strings consists of lowercase English

設計一個字串StringC++練習題

要求:設計一個字串類String,可以求字串長度,可以連線兩個串(如,s1=“計算機”,s2=“軟體”,s1與s2連線得到“計算機軟體”),並且過載“=”運算子進行字串賦值,編寫主程式實現:s1="電腦科學",s2=“是發展最快的科學!”,求s1和s2的串長,連線s1和s2   #incl

LeetCode第87題擾亂字串

原題如下: 給定一個字串 s1,我們可以把它遞迴地分割成兩個非空子字串,從而將其表示為二叉樹。 下圖是字串 s1 = “great” 的一種可能的表示形式。 great / gr eat / \ / g r e at / a

PAT (Advanced Level) Practice 1040 Longest Symmetric String 25 分 最長迴文字串 dp

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int maxn=

1040 Longest Symmetric String 25 分(字串處理)

1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the longest symmetric sub-string. For

String.Utils.join陣列—字串簡單用法

目錄 將陣列轉換為字串: 需要引入包:import org.apache.commons.lang3.StringUtils; //將陣列轉換為字串 StringUtils.join(s

Leetcode演算法題C語言17--驗證迴文字串

題目:驗證迴文字串 給定一個字串,驗證它是否是迴文串,只考慮字母和數字字元,可以忽略字母的大小寫。 說明:本題中,我們將空字串定義為有效的迴文串。 示例 1: 輸入: “A man, a plan

LeetCode演算法題-Reverse StringJava實現

這是悅樂書的第205次更新,第217篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第73題(順位題號是344)。編寫一個以字串作為輸入並返回字串的函式。例如: 輸入:“hello” 輸出:“olleh” 輸入:“A man, a plan

LeetCode演算法題-First Unique Character in a StringJava實現

這是悅樂書的第213次更新,第226篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第81題(順位題號是387)。給定一個字串,找到它中的第一個非重複字元並返回它的索引。 如果它不存在,則返回-1。例如: 輸入:“leetcode” 輸出:0 輸入:“loveleetc

[LeetCode] Interleaving String 交織相錯的字串

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbc

LeetCode演算法題-Number of Segments in a StringJava實現

這是悅樂書的第226次更新,第239篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第93題(順位題號是434)。計算字串中的段數,其中段定義為非空格字元的連續序列。請注意,該字串不包含任何不可列印的字元。例如: 輸入:“Hello, my name is John”

LeetCode演算法題-Find All Anagrams in a StringJava實現

這是悅樂書的第228次更新,第240篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第95題(順位題號是438)。給定一個字串s和一個非空字串p,找到s中p的字謎的所有起始索引。字串僅由小寫英文字母組成,字串s和p的長度不會大於20,100。輸出順序無關緊要。例如: 輸入

Leetcode演算法題C語言15--字串中的第一個唯一字元

題目:字串中的第一個唯一字元 給定一個字串,找到它的第一個不重複的字元,並返回它的索引。如果不存在,則返回 -1。 案例: s = “leetcode” 返回 0. s = “loveleetco

leetcode - Interleaving String

padding == color find family -h data- tom -s Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For exampl

LeetCode-Interleaving String[dp]

pub 分析 adb med 字符串 return code != 字符 Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. F