Open In App

JavaScript Program to Print all Possible Strings that can be Made by Placing Spaces

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'

Example: This example shows the implementation of the above-explained approach.




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.

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.




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.

Example: This example shows the implementation of the above-explained approach.




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)


Article Tags :