From 3a79f33df42fcf344972d58a83f47990bd9ce054 Mon Sep 17 00:00:00 2001 From: Akshat-Raii Date: Sat, 19 Oct 2024 23:06:44 +0530 Subject: [PATCH 1/2] feat:added Word Break problem in backtracking --- Backtracking/WordBreak.js | 36 ++++++++++++++++++++++++++++ Backtracking/tests/WordBreak.test.js | 34 ++++++++++++++++++++++++++ package-lock.json | 7 +++--- 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 Backtracking/WordBreak.js create mode 100644 Backtracking/tests/WordBreak.test.js diff --git a/Backtracking/WordBreak.js b/Backtracking/WordBreak.js new file mode 100644 index 0000000000..bb1598ea71 --- /dev/null +++ b/Backtracking/WordBreak.js @@ -0,0 +1,36 @@ +export class WordBreakSolution { + // Function to determine if the input string 's' can be segmented into words from the 'wordDict' + wordBreak(s, wordDict) { + const wordSet = new Set(wordDict) // Convert wordDict into a set for efficient lookups + const memo = Array(s.length).fill(null) // Initialize memoization array to store results of subproblems + return this.canBreak(0, s, wordSet, memo) // Start the recursive function from the 0th index + } + + // Helper function to perform recursive backtracking with memoization + canBreak(start, s, wordSet, memo) { + if (start === s.length) { + return true // If we reach the end of the string, return true as we successfully segmented it + } + + if (memo[start] !== null) { + return memo[start] // Return the cached result if already computed for this index + } + + // Explore all possible substrings starting from 'start' index + for (let end = start + 1; end <= s.length; end++) { + const currentSubstring = s.slice(start, end) // Get the substring from 'start' to 'end' + + // If the current substring is a valid word and the rest of the string can be broken, return true + if ( + wordSet.has(currentSubstring) && + this.canBreak(end, s, wordSet, memo) + ) { + memo[start] = true // Cache the result as true for this index + return true + } + } + + memo[start] = false // Cache the result as false if no valid segmentation found + return false + } +} diff --git a/Backtracking/tests/WordBreak.test.js b/Backtracking/tests/WordBreak.test.js new file mode 100644 index 0000000000..e71d13f74e --- /dev/null +++ b/Backtracking/tests/WordBreak.test.js @@ -0,0 +1,34 @@ +import { describe, it, expect } from 'vitest' +import { WordBreakSolution } from '../WordBreak' + +describe('Word Break Algorithm', () => { + it('should return true for valid word segmentation', () => { + const solution = new WordBreakSolution() + const result = solution.wordBreak('leetcode', ['leet', 'code']) + expect(result).toBe(true) + }) + + it('should return false for invalid word segmentation', () => { + const solution = new WordBreakSolution() + const result = solution.wordBreak('applepenapple', ['apple', 'pen']) + expect(result).toBe(true) + }) + + it('should handle edge cases with empty strings', () => { + const solution = new WordBreakSolution() + const result = solution.wordBreak('', ['leet', 'code']) + expect(result).toBe(true) + }) + + it('should return false when no word break is possible', () => { + const solution = new WordBreakSolution() + const result = solution.wordBreak('catsandog', [ + 'cats', + 'dog', + 'sand', + 'and', + 'cat' + ]) + expect(result).toBe(false) + }) +}) diff --git a/package-lock.json b/package-lock.json index 5c38ba06a8..9402658c3c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1505,11 +1505,12 @@ } }, "node_modules/micromatch": { - "version": "4.0.5", + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", "dev": true, - "license": "MIT", "dependencies": { - "braces": "^3.0.2", + "braces": "^3.0.3", "picomatch": "^2.3.1" }, "engines": { From 8da81b90945e6c3af9d08db0b96a399b9b59f773 Mon Sep 17 00:00:00 2001 From: Akshat-Raii Date: Sat, 19 Oct 2024 23:20:27 +0530 Subject: [PATCH 2/2] feat:added Word Break problem in backtracking --- Backtracking/WordBreak.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Backtracking/WordBreak.js b/Backtracking/WordBreak.js index bb1598ea71..450fecb448 100644 --- a/Backtracking/WordBreak.js +++ b/Backtracking/WordBreak.js @@ -1,3 +1,11 @@ +/** + * Determines if the input string can be segmented into words from the provided dictionary. + * @param {string} s - The input string to be segmented. + * @param {string[]} wordDict - An array of valid words for segmentation. + * @returns {boolean} True if the string can be segmented into valid words, false otherwise. + * @see https://www.geeksforgeeks.org/word-break-problem-using-backtracking/ + */ + export class WordBreakSolution { // Function to determine if the input string 's' can be segmented into words from the 'wordDict' wordBreak(s, wordDict) {