Wawancara 150 TeratasSedang

Istirahat Kata

Panduan terperinci dan implementasi Python untuk masalah 'Word Break'.

Pernyataan Masalah

Sedang

Diberikan string s dan kamus string wordDict, return True if s dapat disegmentasikan ke dalam rangkaian satu atau lebih kata kamus yang dipisahkan spasi.

Perhatikan bahwa kata yang sama dalam kamus dapat digunakan kembali beberapa kali dalam segmentasi.

Tulis fungsi wordBreak(s: str, wordDict: List[str]) -> bool.

Kendala
  • 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

Contoh

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?
Pertimbangkan untuk menggunakan struktur data khusus DP 1D seperti kumpulan atau tumpukan.
Edge Cases to Watch
  • Struktur masukan kosong
  • Masukan elemen tunggal
  • Batasan angka yang besar

Siap Memecahkannya?

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

Buka di Editor
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

Sumber Daya Python yang Direkomendasikan

Perluas pengetahuan Anda dengan tutorial interaktif terkait, lembar contekan, dan perbandingan kode.