
- 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
Get all substrings of a string in JavaScript recursively
In the provided problem statement, our aim is to get all substrings of a string recursively with the help of Javascript. So here we will be creating a recursive function that can generate all possible substrings of a given input string.
Understanding the problem statement
The problem statement is asking us to create a recursive function which can generate all the possible substrings of a given input string with the help of Javascript. A substring is any continuous sequence of characters within the input string. For example: input strings “xy" so the possible substrings are "x", "y", and "xy".
Algorithm
Step 1 − Start the program by defining the function and pass an argument in it a string to which we have to find the substrings.
Step 2 − Define an array to hold the result array of substrings. And initialize it with zero values.
Step 3 − Use another recursive function and pass two parameters in it as starting Index and ending Index.
Step 4 − Declare a base case and check the condition that the ending Index is equal to the string length. If it is equal then return.
Step 5 − Use a recursive case in which we will check that the starting index is greater than the ending index then call the recurse function again and increment the ending index by 1.
Step 6 − Otherwise push the result by slicing the string and pass startIndex and endIndex. And again call the recurse function.
Step 7 − Call the recurse function and return the result.
Code for the algorithm
// function to get the substrings for the given string function getAllSubstrings(str) { let result = []; function recurse(startIndex, endIndex) { // Base case if (endIndex === str.length) { return; } // Recursive case if (startIndex > endIndex) { recurse(0, endIndex + 1); } else { result.push(str.slice(startIndex, endIndex + 1)); recurse(startIndex + 1, endIndex); } } recurse(0, 0); return result; } const inputStr = "wxyz"; const subStr = getAllSubstrings(inputStr); console.log(subStr);
Complexity
The above recursive function has a time complexity of O(n^2) where n is the length of the input string. Because the function produces all possible substrings from the given input string. Additionally we contain (n^2) substrings in memory so the code has an O(n^2) space complexity.
Conclusion
In the Javascript code we have utilized a recursive strategy to locate all the possible substrings of the supplied string. With the usage of nested function the code is generating all the potential strings. The recurse function executes with different parameters until no substrings are created.
- Related Articles
- Find all substrings in a string using C#
- Lexicographic rank of a string among all its substrings
- Segregating a string into substrings - JavaScript
- C# Program to find all substrings in a string
- Program to print all substrings of a given string in C++
- Is the string a combination of repeated substrings in JavaScript
- Count Unique Characters of All Substrings of a Given String in C++
- Unique substrings in circular string in JavaScript
- How to List all Substrings in a given String using C#?
- Find all substrings combinations within arrays in JavaScript
- Adding paragraph tag to substrings within a string in JavaScript
- Counting substrings of a string that contains only one distinct letter in JavaScript
- Recursively adding digits of a number in JavaScript
- Separate a string with a special character sequence into a pair of substrings in JavaScript?
- Minimum Changes to string to make all substrings distinct
