150 najlepszych wywiadówŚredni

Przeplatanie ciągu

Szczegółowy przewodnik i implementacja Python dla problemu „Przeplatania ciągu”.

Oświadczenie o problemie

Średni

Mając dane ciągi s1, s2 i s3, sprawdź, czy s3 powstaje w wyniku przeplatania s1 i s2.

Przeplatanie dwóch ciągów s i t to konfiguracja, w której są one podzielone na niepuste podciągi w taki sposób, że s = s1 + s2 + ... + sn, t = t1 + t2 + ... + tm, |n - m| <= 1, a przeplatany ciąg to s1 + t1 + s2 + t2 + ... lub t1 + s1 + t2 + s2 + ...

Napisz funkcję isInterleave(s1: str, s2: str, s3: str) -> bool.

Ograniczenia
  • 0 <= len(s1), len(s2) <= 100
  • 0 <= len(s3) <= 200
  • s1, s2, and s3 consist of lowercase English letters

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla 2D DP, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.