ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Generate All Permutations of a String

author
Generated by
Anushka Agrawal

13/10/2024

Java

Sign in to read full article

Generating all permutations of a string might sound like a straightforward task, but it introduces a fascinating area of recursion and backtracking in algorithms. This concept is not just critical for theoretical knowledge but also proves useful in practical applications like cryptography and combinatorial problems.

Understanding Permutations

A permutation of a string is a rearrangement of its characters. For example, the string "abc" has the following permutations:

  • abc
  • acb
  • bac
  • bca
  • cab
  • cba

As you can see from this small example, the number of permutations grows factorially with the length of the string (n!). This means that for longer strings, the task becomes exponentially more complex.

Recursive Approach to Generate Permutations

The core idea behind generating permutations is to fix one character at a time and recursively find the permutations of the rest of the characters. This is where recursion shines. If we take the first character and generate all permutations of the remaining string, we can easily form permutations by attaching the fixed character at each possible position.

Example Breakdown

Let's break down the approach using the string "abc":

  1. Fix the first character ('a').
  2. Generate permutations of the remaining substring ('bc'):
    • bc → b, c
    • Now our permutations become: ab, ac.
  3. Next, fix 'b':
    • Generate permutations of remaining ('ac'):
      • ac → a, c
      • New permutations: ba, bc.
  4. Finally, fix 'c':
    • Generate permutations of ('ab'):
      • ab → a, b
      • Results in: ca, cb.

Following this pattern leads to all permutations.

Backtracking for Permutation Generation

Backtracking aids us in exploring possible configurations and retracting them as necessary. When generating permutations, if a character has been used, we skip it during that permutation’s formation.

Java Implementation

Let's implement this in Java using recursion and backtracking. We’ll create a function that accepts a string and initiates the permutation process.

import java.util.ArrayList; import java.util.List; public class PermutationGenerator { public List<String> generatePermutations(String str) { List<String> result = new ArrayList<>(); generatePermutations(str.toCharArray(), 0, result); return result; } private void generatePermutations(char[] chars, int index, List<String> result) { if (index == chars.length - 1) { result.add(new String(chars)); return; } for (int i = index; i < chars.length; i++) { swap(chars, index, i); generatePermutations(chars, index + 1, result); swap(chars, index, i); // backtrack } } private void swap(char[] chars, int i, int j) { char temp = chars[i]; chars[i] = chars[j]; chars[j] = temp; } public static void main(String[] args) { PermutationGenerator generator = new PermutationGenerator(); String input = "abc"; List<String> permutations = generator.generatePermutations(input); System.out.println(permutations); } }

Explanation of the Code

  1. generatePermutations: This is the main method that initializes the process. It turns the input string into an array of characters and initializes the results list.

  2. Recursive Method: generatePermutations takes the character array and starts generating permutations from the current index.

  3. Check Base Case: If the current index reaches the last character, we have a valid permutation which is added to the results list.

  4. Swap and Recursion: We iterate through the characters starting from the current index, swap each character with the current index, and generate permutations recursively by moving to the next index.

  5. Backtracking: After recursion, we swap back to restore the original state of the array, enabling the next iteration to proceed correctly.

Complexity Analysis

  • The time complexity of generating permutations is O(n * n!), where n is the length of the string. This arises because for each of the n! permutations, we perform O(n) operations (to generate the string).
  • The space complexity is O(n) due to the recursion stack and the space used to store the permutations.

Understanding the structure and logic behind permutation generation using recursion and backtracking can significantly improve your skills in problem-solving and algorithm design. With strings being a fundamental data type, knowing how to manipulate them through permutations opens doors to numerous applications and challenges in programming. Happy coding!

Popular tags

JavaDSARecursion

Share now!

Like & bookmark

Related collections

  • Advanced Dynamic Programming Interview Questions

    15/11/2024 · DSA

  • Top 20 DSA Interview Questions Mastery

    23/09/2024 · DSA

  • Advanced Recursion and Backtracking Problems Using Java

    13/10/2024 · DSA

  • Advanced Graph Interview Questions in Java

    16/11/2024 · DSA

  • Mastering Arrays : The Basic Data Structure

    06/12/2024 · DSA

Related articles

  • Finding the Maximum Subarray Sum with Kadane’s Algorithm

    06/12/2024 · DSA

  • Finding the Median of a Stream Using Heaps

    16/11/2024 · DSA

  • Understanding Bipartite Graphs and Matching Problems in DSA

    16/11/2024 · DSA

  • Smallest Substring Containing All Characters of Another String

    15/11/2024 · DSA

  • Finding the Longest Increasing Subsequence

    15/11/2024 · DSA

  • Understanding Arrays and Strings in Data Structures and Algorithms

    06/12/2024 · DSA

  • The Knapsack Problem

    15/11/2024 · DSA

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design