🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🔧 AI Nachrichten ChatGPT automatically logged out [Fix](12.09.2026 um 17:09 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
🪟 Windows TippsServertimeout in Outlook über 10 Minuten verlängern(12.09.2026 um 15:10 Uhr)
🔧 AI Nachrichten Stealing AI Reasoning Traces(08.09.2026 um 12:20 Uhr)
🔧 AI Nachrichten AIs as Modern Genies(08.09.2026 um 19:12 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🔧 AI Nachrichten ChatGPT automatically logged out [Fix](12.09.2026 um 17:09 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
🪟 Windows TippsServertimeout in Outlook über 10 Minuten verlängern(12.09.2026 um 15:10 Uhr)
🔧 AI Nachrichten Stealing AI Reasoning Traces(08.09.2026 um 12:20 Uhr)
🔧 AI Nachrichten AIs as Modern Genies(08.09.2026 um 19:12 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

LeetCode Meditations: Word Break

↗ Quelle (dev.to)
🗣️ Stimme:

The description for where we build a dp array to track whether it is possible to break s into words in wordDict at each index.



Each index


ii i

in the dp array will indicate whether it is possible to break the entire string into words starting at index

ii i

.














Note

dp needs to be of size s.length + 1 to hold the edge case of an empty string, in other words, when we're out of bounds.


Let's create it with initially false values:




CODE
let dp = Array.from({ length: s.length + 1 }, () => false); // +1 for the base case, out of bounds






The last index is the empty string, which can be considered breakable, or in other words, valid:




CODE
dp[s.length] = true; // base case






Going backwards, for each index of s, we can check if any word in wordDict can be reached from that index onwards:




CODE
for (let i = s.length - 1; i >= 0; i--) {
for (const word of wordDict) {
/* ... */
}
}






If we're still within bounds of s (i + word.length <= s.length) and we find the word (s.slice(i, i + word.length) === word), we'll mark that slot as the truth value of the "next position" that we can break the string, which will be i + word.length:




CODE
for (let i = s.length - 1; i >= 0; i--) {
for (const word of wordDict) {
if (i + word.length <= s.length && s.slice(i, i + word.length) === word) {
dp[i] = dp[i + word.length];
}
/* ... */
}
}






If we can break it into any word in wordDict, we don't have to keep looking at other words, so we can just break out of the loop:




CODE
for (let i = s.length - 1; i >= 0; i--) {
for (const word of wordDict) {
if (i + word.length <= s.length && s.slice(i, i + word.length) === word) {
dp[i] = dp[i + word.length];
}
if (dp[i]) {
break;
}
}
}






Finally, we return dp[0] — if the whole string is breakable into words in wordDict, its value will store true, otherwise false:




CODE
function wordBreak(s: string, wordDict: string[]): boolean {
/* ... */
return dp[0];
}






And, here is the final solution:




CODE
function wordBreak(s: string, wordDict: string[]): boolean {
let dp = Array.from({ length: s.length + 1 }, () => false); // +1 for the base case, out of bounds
dp[s.length] = true; // base case

for (let i = s.length - 1; i >= 0; i--) {
for (const word of wordDict) {
if (i + word.length <= s.length && s.slice(i, i + word.length) === word) {
dp[i] = dp[i + word.length];
}
if (dp[i]) {
break;
}
}
}

return dp[0];
}









Time and space complexity



The time complexity is

O(n∗m∗t)O(n * m * t) O(nmt)

where

nn n

is the string s,

mm m

is the number of words in wordDict, and

tt t

is the maximum length word in wordDict — as we have a nested loop that runs through each word in wordDict with a slice operation that uses word.length for each character in s.



The space complexity is

O(n)O(n) O(n)

because of the dp array we store for each index of s.






The last dynamic programming problem in the series will be Longest Increasing Subsequence. Until then, happy coding.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
The Gemini desktop app is now available for Windows
1 Quelle
ChatGPT automatically logged out [Fix]
1 Quelle
Windows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten LeetCode Meditations: Word Break

Thematisch verwandte Begriffe: LeetCode, Meditations, Word, Break · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...