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

Understanding How Websites Work and How HTML, CSS, and JavaScript Contribute

Introduction: In today's digital age, websites have become an integral part of our lives. From simple blogs to complex e-commerce platforms, websites have revolutionized the way we interact, communicate, and conduct business online. Behind the scenes, three fundamental technologies play a crucial role in the creation and functionality of websites: HTML, CSS, and JavaScript. In this blog post, we will delve into the workings of websites, exploring the role of each of these technologies and how they contribute to the overall web experience. HTML: The Structure of the Web: HTML (Hypertext Markup Language) forms the backbone of web pages. It is a markup language that defines the structure and content of a webpage. HTML uses tags to enclose various elements, such as headings, paragraphs, images, links, and more. These tags provide a structure and meaning to the content, allowing web browsers to interpret and display them correctly. For example, a simple HTML structure for a webpage migh...

JavaScript Basics

Writing a program in JavaScript: Syntax: the rules to write code Logic: the code to process data Input: the data given for processing Output: the processed data Printing a string in console: console.log("Hello World"); JavaScript Identifier: Identifier is a name that we give to any reference (for variables, constants, functions, etc.) Any existing JavaScript keyword can not be an identifier. The first letter of an identifier must be either a letter (A-Z, a-z) or an underscore (_), or a dollar sign ($). The rest of the characters can be letters, digits, underscore, or dollar signs. JavaScript variables: JavaScript variables can be defined using 2 keywords. "var" and "let". var identifierName = "Hello"; let identifierName = "Hello"; Difference between var and let: The term "let" was introduced in ES6 (ECMAScript 6) with additional features. The first difference is the scope of the variable. Variables defined as var will have glo...