Skip to main content

Find All Permutations of any number in JavaScript.

Question: 

Given a string S of length N. 

Print all permutations of the string in separate lines.

Input Size: 1 <= N <= 100000

Sample Testcases :

INPUT:
123

OUTPUT:
123
231
321
213
312
132

Answer: 

Steps to find all permutations of any number:

  1. We are going to use Recursion for calculating the permutations. Create a method called "getPermutations", and create a Permutations array inside the method that will hold all the permutations. Let's start with the base condition (Where the recursion stops).
  2. The base condition will be when the number has only one digit simply add it to the permutations array and return the array.
  3. If the number has more digits, then iterate through all the digits.
  4. On each iteration, find the current digit (store it in one variable) and find all other digits (store it in another variable).
  5. Call the recursive function "getPermutations" and pass the other digits variable created in the above step. We will expect this to return an array of all permutations of the other digits variable.
  6. For each permutation received, append to the current digit and add it to the Permutations array.
  7. Since we are iterating the actual number, this will be done for all digits.
  8. Outside the for loop simply return the Permutations array.



Comments

Popular posts from this blog

Evaluating Postfix expressions in JavaScript

  Question: Postfix expression evaluation A string containing the postfix expression is given to you. Evaluate it and print the single integer giving the answer. Input Description: The first line of the input is a string N, containing operators and numbers separated by a single space which forms a postfix expression. Output Description: Evaluate the post expression and print the result. Sample Input: 5 3 1 * + 9 - Sample Output: -1 Answer:  Steps to evaluate a prefix expression: Split the prefix expression based on space (" ") or blank string ("") as per the given input. Store the split characters in an array. Create an evaluation array with no elements (blank array). Iterate through the elements of the characters array. If the character is a number, simply push the character into the evaluation array. if the character is an operator (+, -, *, /, %), then pop 2 elements from the evaluation array. Store the first popped number as the right Operand and the second as t...

Mastering Git: The Ultimate Guide to Version Control with VSCode

Introduction: In the world of software development, efficient collaboration, and seamless version control are essential for successful project management. Git, a distributed version control system, has revolutionized the way developers work by providing a robust and flexible solution. In this blog post, we will explore the fundamentals of Git, its significance in modern software development, and how to leverage Git commands using the popular IDE, Visual Studio Code (VSCode). What is Git and Why is it Used? Git is a free and open-source distributed version control system designed to handle everything from small to large projects with speed and efficiency. It allows multiple developers to work on the same project simultaneously, providing seamless integration and a structured approach to managing changes. Git offers several key benefits, including: Distributed Version Control: Each developer has a complete copy of the project's history, enabling them to work offline and independentl...

HTTP 1.1 vs HTTP 2

HTTP 1.1 vs HTTP 2 Introduction: HTTP (Hypertext Transfer Protocol) is the underlying protocol that powers the World Wide Web. It is a request-response protocol that enables communication between clients and servers. Over the years, HTTP has evolved and seen several versions. The two most widely used versions of HTTP are HTTP1.1 and HTTP2. In this blog, we will explore the differences between HTTP1.1 and HTTP2. Multiplexing: One of the significant differences between HTTP1.1 and HTTP2 is that HTTP2 supports multiplexing, while HTTP1.1 doesn't. Multiplexing allows multiple requests and responses to be sent and received concur...