In this article, we are going to learn about getting all substrings of the given string by using javascript, Substrings of a string are contiguous sequences of characters obtained by selecting a range of characters from the original string. They can be of various lengths and positions within the string.
Example:
Input : abc
Output : [ 'a', 'ab', 'abc', 'b', 'bc', 'c' ]
There are several methods that can be used to get all substrings of the given string in JavaScript, which are listed below:
- Using recursive
- Using Array.reduce() and Array.concat()
- Using Nested Loops
We will explore all the above methods along with their basic implementation with the help of examples.
Approach 1: Using Recursive Approach
In this approach, the Recursive function generates all substrings. Split the string into the first character and rest, then combine them.
Syntax:
function SubstringFunction(input) {
if (input.length === 0) {
return [''];
}
let first = input[0];
let restSubstrings = SubstringFunction(input.slice(1));
let substrings = restSubstrings.map(sub => first + sub);
return [...substrings, ...restSubstrings];
}
Example: In this example we are using the above-explained approach.
Javascript
function SubstringFunction(input) { if (input.length === 0) { return [ '' ]; } let first = input[0]; let restSubstrings = SubstringFunction(input.slice(1)); let substrings = restSubstrings.map(sub => first + sub); return [...substrings, ...restSubstrings]; } let str1 = "abc" ; let result = SubstringFunction(str1); console.log(result); |
[ 'abc', 'ab', 'ac', 'a', 'bc', 'b', 'c', '' ]
Approach 2: Using Array.reduce() and Array.concat() Methods
In this approach, The substringFunction splits input, then reduce() accumulates substrings by slicing the string. Array.from() creates substrings progressively, and concat() merges them into the final array.
Syntax:
function substringFunction(input) {
return input.split('').reduce((substrings, _, i) =>
substrings.concat(Array.from(
{ length: input.length - i },
(_, j) => input.slice(i, i + j + 1))),
[]
);
};
Example: In this example we are using the above-explained approach.
Javascript
function substringFunction(input) { return input.split( '' ).reduce((substrings, _, i) => substrings.concat(Array.from( { length: input.length - i }, (_, j) => input.slice(i, i + j + 1))), [] ); } let str1 = "abc" ; let result = substringFunction(str1); console.log(result); |
[ 'a', 'ab', 'abc', 'b', 'bc', 'c' ]
Approach 3: Using Nested Loops
In this approach,we are using nested loops, iterate through input string. Capture substrings from each character to end, appending to accumulate all possible substrings.
Syntax:
for (let i = 0; i < input.length; i++) {
for (let j = i + 1; j <= input.length; j++) {
substrings.push(input.slice(i, j));
}
};
Example: In this approach we are using the above-explained approach.
Javascript
function substringFunction(input) { let substrings = []; for (let i = 0; i < input.length; i++) { for (let j = i + 1; j <= input.length; j++) { substrings.push(input.slice(i, j)); } } return substrings; } let str1 = "abc" ; let result = substringFunction(str1); console.log(result); |
[ 'a', 'ab', 'abc', 'b', 'bc', 'c' ]