
g0101_0200.s0139_word_break.solution.ts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
The newest version!
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table
// #Dynamic_Programming #Trie #Memoization #Algorithm_II_Day_15_Dynamic_Programming
// #Dynamic_Programming_I_Day_9 #Udemy_Dynamic_Programming #Big_O_Time_O(M+max*N)_Space_O(M+N+max)
// #2023_10_06_Time_56_ms_(88.44%)_Space_44.1_MB_(93.39%)
function wordBreak(s: string, wordDict: string[]): boolean {
const dp: boolean[] = []
for (let i = 0; i <= s.length; i++) dp.push(false)
dp[s.length] = true
for (let j = s.length - 1; j >= 0; j--) {
for (const word of wordDict) {
if (s.slice(j, j + word.length) === word && j + word.length <= s.length) {
dp[j] = dp[j + word.length]
}
if (dp[j]) break
}
}
return dp[0]
}
export { wordBreak }
© 2015 - 2025 Weber Informatics LLC | Privacy Policy