150 najlepszych wywiadówŚredni

Przerwa na słowo

Szczegółowy przewodnik i implementacja Python dla problemu „Podział wyrazów”.

Oświadczenie o problemie

Średni

Biorąc pod uwagę ciąg s i słownik ciągów wordDict, zwróć wartość True, jeśli s można podzielić na oddzieloną spacjami sekwencję jednego lub większej liczby słów ze słownika.

Należy pamiętać, że to samo słowo w słowniku może zostać użyte wielokrotnie w segmentacji.

Napisz funkcję wordBreak(s: str, wordDict: List[str]) -> bool.

Ograniczenia
  • 1 <= len(s) <= 300
  • 1 <= len(wordDict) <= 1000
  • 1 <= len(wordDict[i]) <= 20
  • s and wordDict[i] consist of only lowercase English letters
  • All the strings of wordDict are unique

Przykłady

Example 1
Input
s = "leetcode", wordDict = ["leet","code"]
Output
True
Explanation

Return True because "leetcode" can be segmented as "leet code".

Example 2
Input
s = "applepenapple", wordDict = ["apple","pen"]
Output
True
Explanation

Return True because "applepenapple" can be segmented as "apple pen apple".

Example 3
Input
s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
Output
False
Explanation

No valid segmentation exists.

Need a Hint?
Rozważ użycie struktur danych specyficznych dla 1D 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.