JavaScript Program to Print all Possible Strings that can be Made by Placing Spaces
Last Updated :
06 Oct, 2023
In this article, We are going to see how can we print all possible strings that can be made by placing spaces. We will add spaces in between the strings and print every possible value.
Example:
Input: "ABCD"
Output: 'A B C D'
'A B CD'
'A BC D'
'A BCD'
'AB C D'
'AB CD'
'ABC D'
'ABCD'
Approach 1: By generating Permutations
- The idea is to use two methods, the first permutation method initializes an object and sorts the generated permutations.
- The second is solve method which is a recursive helper function that builds permutations by either including or excluding characters from the input string.
Example: This example shows the implementation of the above-explained approach.
Javascript
class Solution {
permutation(s) {
let obj = { "k" : [] };
this .solve(s, "" , obj);
return obj[ "k" ].sort();
}
solve(s, op, obj) {
if (s.length === 0) {
obj[ "k" ].push(op);
return ;
}
let ip = s[0];
s = s.slice(1);
this .solve(s, op + ip, obj);
if (op.length > 0) {
this .solve(s, op + " " + ip, obj);
}
}
}
const solution = new Solution();
const input = "ABC" ;
const result = solution.permutation(input);
console.log(result);
|
Output
[ 'A B C', 'A BC', 'AB C', 'ABC' ]
Time Complexity: O(N!), where N is the length of the input string.
Space Complexity: O(N!), in worst case since the space grows with the number of permutations.
Approach 2: By placing spaces by Recursion
This code generates all permutations of a given string ‘s’ using recursion. It starts with an empty output and two branches for each character in ‘s’: one where the character is included in the current permutation and one where it’s excluded (represented by adding a space). This process continues recursively until all permutations are generated and returned as an array.
Example: This example shows the implementation of the above-explained approach.
Javascript
class Solution {
permutation(s) {
return this .subSet(s.charAt(0), s.slice(1));
}
subSet = (output, input) => {
let arr = [];
if (input.length == 0) {
arr.push(output);
return arr;
}
let output1 = output + input.charAt(0);
let output2 = output + " " + input.charAt(0);
arr =
arr.concat( this .subSet(output2, input.slice(1)));
arr =
arr.concat( this .subSet(output1, input.slice(1)));
return arr;
};
}
const solution = new Solution();
const input = "ABCD" ;
const result = solution.permutation(input);
console.log(result);
|
Output
[
'A B C D', 'A B CD',
'A BC D', 'A BCD',
'AB C D', 'AB CD',
'ABC D', 'ABCD'
]
Time Complexity: O(N!), where N is the length of the input string.
Space Complexity: O(N!), in worst case since the space grows with the number of permutations.
Approach 3: By Recursion with Space Optimization
- The approach is to generate all possible permutations of a given string by recursively branching at each character, creating two options.
- One with the character joined to the previous characters and one with a space before the character.
- It continues this process for each character, and when it reaches the end, it adds the generated permutation to the result array.
- The final result is a sorted array of all possible permutations.
Example: This example shows the implementation of the above-explained approach.
Javascript
class Solution {
permutation(s) {
if (s.length === 0) return [s];
const res = [];
let temp = '' ;
temp += s[0];
let input = s.slice(1);
const genPer = (temp, input) => {
if (input.length === 0) {
res.push(temp);
return ;
}
let op1 = temp;
let op2 = temp;
op1 += input[0];
op2 += ' ' + input[0];
let clone = input.slice(1);
genPer(op1, clone);
genPer(op2, clone);
};
genPer(temp, input);
return res.sort();
}
}
const solution = new Solution();
const input = "ABC" ;
const result = solution.permutation(input);
console.log(result);
|
Output
[ 'A B C', 'A BC', 'AB C', 'ABC' ]
Time Complexity: O(N!), where N is the length of the input string.
Space Complexity: O(N)
Share your thoughts in the comments
Please Login to comment...