
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get all substrings of a string in JavaScript recursively
We are required to write a JavaScript function that takes in a string as the only argument. The function should recursively construct all possible substrings of the input string.
Then the function should return an array containing all the substrings.
Example
const str = 'example'; const buildSubstrings = (str = '') => { let i, j; const res = []; for (i = 0; i < str.length; i++) { for (j = i + 1; j < str.length + 1; j++) { res.push(str.slice(i, j)); }; }; return res; }; console.log(buildSubstrings(str));
Output
And the output in the console will be −
[ 'e', 'ex', 'exa', 'exam', 'examp', 'exampl', 'example', 'x', 'xa', 'xam', 'xamp', 'xampl', 'xample', 'a', 'am', 'amp', 'ampl', 'ample', 'm', 'mp', 'mpl', 'mple', 'p', 'pl', 'ple', 'l', 'le', 'e' ]
- Related Questions & Answers
- Find all substrings in a string using C#
- Segregating a string into substrings - JavaScript
- Program to print all substrings of a given string in C++
- Count Unique Characters of All Substrings of a Given String in C++
- C# Program to find all substrings in a string
- Is the string a combination of repeated substrings in JavaScript
- Unique substrings in circular string in JavaScript
- How to List all Substrings in a given String using C#?
- Recursively adding digits of a number in JavaScript
- Find all substrings combinations within arrays in JavaScript
- Adding paragraph tag to substrings within a string in JavaScript
- Replacing Substrings in a Java String
- Program to find total sum of all substrings of a number given as string in Python
- Counting substrings of a string that contains only one distinct letter in JavaScript
- Separate a string with a special character sequence into a pair of substrings in JavaScript?
Advertisements