Skip to content

Commit 3a79f33

Browse files
committed
feat:added Word Break problem in backtracking
1 parent 55ff0ad commit 3a79f33

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

Backtracking/WordBreak.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export class WordBreakSolution {
2+
// Function to determine if the input string 's' can be segmented into words from the 'wordDict'
3+
wordBreak(s, wordDict) {
4+
const wordSet = new Set(wordDict) // Convert wordDict into a set for efficient lookups
5+
const memo = Array(s.length).fill(null) // Initialize memoization array to store results of subproblems
6+
return this.canBreak(0, s, wordSet, memo) // Start the recursive function from the 0th index
7+
}
8+
9+
// Helper function to perform recursive backtracking with memoization
10+
canBreak(start, s, wordSet, memo) {
11+
if (start === s.length) {
12+
return true // If we reach the end of the string, return true as we successfully segmented it
13+
}
14+
15+
if (memo[start] !== null) {
16+
return memo[start] // Return the cached result if already computed for this index
17+
}
18+
19+
// Explore all possible substrings starting from 'start' index
20+
for (let end = start + 1; end <= s.length; end++) {
21+
const currentSubstring = s.slice(start, end) // Get the substring from 'start' to 'end'
22+
23+
// If the current substring is a valid word and the rest of the string can be broken, return true
24+
if (
25+
wordSet.has(currentSubstring) &&
26+
this.canBreak(end, s, wordSet, memo)
27+
) {
28+
memo[start] = true // Cache the result as true for this index
29+
return true
30+
}
31+
}
32+
33+
memo[start] = false // Cache the result as false if no valid segmentation found
34+
return false
35+
}
36+
}

Backtracking/tests/WordBreak.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { WordBreakSolution } from '../WordBreak'
3+
4+
describe('Word Break Algorithm', () => {
5+
it('should return true for valid word segmentation', () => {
6+
const solution = new WordBreakSolution()
7+
const result = solution.wordBreak('leetcode', ['leet', 'code'])
8+
expect(result).toBe(true)
9+
})
10+
11+
it('should return false for invalid word segmentation', () => {
12+
const solution = new WordBreakSolution()
13+
const result = solution.wordBreak('applepenapple', ['apple', 'pen'])
14+
expect(result).toBe(true)
15+
})
16+
17+
it('should handle edge cases with empty strings', () => {
18+
const solution = new WordBreakSolution()
19+
const result = solution.wordBreak('', ['leet', 'code'])
20+
expect(result).toBe(true)
21+
})
22+
23+
it('should return false when no word break is possible', () => {
24+
const solution = new WordBreakSolution()
25+
const result = solution.wordBreak('catsandog', [
26+
'cats',
27+
'dog',
28+
'sand',
29+
'and',
30+
'cat'
31+
])
32+
expect(result).toBe(false)
33+
})
34+
})

package-lock.json

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)