150強訪談中等

交錯字串

“交錯字串”問題的詳細指南和 Python 實作。

問題陳述

中等

給定字串 s1、s2 和 s3,找出 s3 是否由 s1 和 s2 交錯形成。

兩個字串 s 和 t 的交錯是一種配置,其中它們被分成非空子字串,使得 s = s1 + s2 + ... + sn, t = t1 + t2 + ... + tm, |n - m| <= 1,交錯字串為 s1 + t1 + s2 + t2 + ... m| <= 1,交錯字串為 s1 + t1 + s2 + t2 + ... 或 t1 + s1 + t2 ...

寫一個函數 isInterleave(s1: str, s2: str, s3: str) -> bool

約束條件
  • 0 <= len(s1), len(s2) <= 100
  • 0 <= len(s3) <= 200
  • s1, s2, and s3 consist of lowercase English letters

範例

Example 1
Input
s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output
True
Explanation

aadbbcbcac can be formed by interleaving "aabcc" and "dbbca".

Example 2
Input
s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output
False
Explanation

It is impossible to interleave s1 and s2 to obtain s3.

Need a Hint?
考慮使用 2D DP 特定的資料結構,例如集合或堆疊。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。