
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Can the string be segmented in JavaScript
We are given a non−empty string str and an array of strings arr containing a list of non−empty words.
We are required to write a function that determines if str can be segmented into a spaceseparated sequence of one or more words that exist in the array.
Note
The same word in the array may be reused multiple times in the segmentation.
The array does not contain duplicate words.
Example 1
If the input is
const str = "applepenapple"; const arr = ["apple", "pen"];
The output should be true, because
"applepenapple" can be segmented as "apple pen apple".
Example
The code for this will be −
const str = "applepenapple"; const arr = ["apple", "pen"]; const wordSequence = (str = '', arr = []) => { const map = {} function helper(str) { if (map.hasOwnProperty(str)) { return map[str] } else if (str=='') { return true } for (let i=0;i<=str.length;i++) { if ( arr.includes(str.slice(i)) && helper(str.slice(0, i)) ){ map[str] = true return true } }; map[str] = false; return false; }; return helper(str) }; console.log(wordSequence(str, arr));
Output
And the output in the console will be −
true
- Related Articles
- Checking if a string can be made palindrome in JavaScript
- Can one string be repeated to form other in JavaScript
- Can part of a string be rearranged to form another string in JavaScript
- Counting the number of palindromes that can be constructed from a string in JavaScript
- Checking if one string can be achieved from another with single tweak in JavaScript
- How can Tensorflow be used in the conversion between different string representations?
- Checking if change can be provided in JavaScript
- Check if a string can be repeated to make another string in Python
- How can a String be validated (for alphabets) in java?
- Check whether second string can be formed from characters of first string in Python
- Can JavaScript be used for Android Development?
- How to write a JavaScript function that returns true if a portion of string 1 can be rearranged to string 2?
- In JavaScript, can be use a new line in console.log?
- How can it be possible to invert a string in MySQL?
- How many ways a String object can be created in java?

Advertisements