0
0
DSA Typescriptprogramming~30 mins

Word Break Problem in DSA Typescript - Build from Scratch

Choose your learning style9 modes available
Word Break Problem
📖 Scenario: Imagine you have a long string of letters without spaces, and you want to check if it can be split into meaningful words from a given list. This is like trying to find spaces in a sentence that someone wrote without pressing the spacebar.
🎯 Goal: You will build a program that checks if a given string can be segmented into a sequence of dictionary words.
📋 What You'll Learn
Create a string variable with a specific sentence without spaces
Create an array of strings representing the dictionary words
Write a function that checks if the string can be segmented into dictionary words
Print the result as true or false
💡 Why This Matters
🌍 Real World
This problem is useful in text processing, like splitting words in languages without spaces or in spell checkers.
💼 Career
Understanding this problem helps in roles involving natural language processing, search engines, and software that processes human language.
Progress0 / 4 steps
1
Create the input string
Create a string variable called inputString and set it to "applepenapple".
DSA Typescript
Hint

Use const to create a string variable named inputString.

2
Create the dictionary array
Create an array of strings called dictionary with these exact words: "apple", "pen".
DSA Typescript
Hint

Use const to create an array named dictionary with the words inside square brackets.

3
Write the word break function
Write a function called wordBreak that takes inputString and dictionary as parameters and returns true if inputString can be segmented into words from dictionary, otherwise false. Use a loop and recursion or dynamic programming inside the function.
DSA Typescript
Hint

Use a boolean array dp to keep track of valid breaks. Loop through the string and check substrings against the dictionary.

4
Print the result
Use console.log to print the result of calling wordBreak with inputString and dictionary.
DSA Typescript
Hint

Call console.log(wordBreak(inputString, dictionary)) to see if the string can be segmented.