• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Contact Us

All about code

  • Java
    • Spring
      • Spring Boot
      • Spring Cloud
    • Java Miscellaneous Tips
    • Java Design Patterns
  • Data Structures and Algorithms
    • Algorithms
    • Data Structures
  • Thoughts and Stories
  • AI

How to Get All Permutations of a String

laptop infographic online business 6087062

Introduction

We want to calculate all the possible permutations of a string. A permutation is a new string, containing the same elements but with a different displacement.

Problem Example

Consider the string “ABC”. Its permutations are:

  • ABC
  • ACB
  • BAC
  • BCA
  • CBA
  • CAB

In the next section, we will see an algorithm written in Java to calculate the permutations of the above string of characters.

Calculate All Permutations of a String

The problem can be approached recursively:

  • We define a recursive function taking the string “ABC” as an array of characters, the start index, and the last index as the length of the array minus 1.
  • The base case is when the start and last index are equal. When this happens we have a permutation and we print it.
  • Otherwise, we do a cycle from the start to the last index, and for each step:
    • We swap characters placed on the left and the current value of the index
    • We call the function recursively adding 1 to the start index
    • We swap the characters again.

The steps described above can be implemented like the following code snippet:

private void performAllPermutations(char[] characters, int left, int rigth) {
    if (left == rigth) {
        System.out.println(characters);
    } else {
        for (int i = left; i <= rigth; i++) {
            characters = swapCharacters(characters, left, i);
            performAllPermutations(characters, left + 1, rigth);
            characters = swapCharacters(characters, left, i);
        }
    }
}

@Test
public void stringPermutationTest() throws Exception {
    String toPermute = "ABC";
    char[] toPermuteArray = toPermute.toCharArray();
    performAllPermutations(toPermuteArray, 0, toPermuteArray.length - 1);
}

public char[] swapCharacters(char[] characters, int i, int j) {
    char temp = characters[i];
    characters[i] = characters[j];
    characters[j] = temp;
    return characters;
}

Permutations of a String: Performance Considerations

The time complexity of the above algorithm is in general O(n*n!), since each permutation costs a factorial term, as we know from combinatory mathematics, and must be repeated n times.

Conclusion

In this article, we have seen how to calculate all the possible permutations of a string. The example code is available on GitHub.

Primary Sidebar

Social

  • Facebook
  • LinkedIn
  • Twitter

Archives

  • May 2026
  • April 2026
  • March 2026
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • September 2023
  • May 2023
  • March 2023
  • January 2023
  • November 2022
  • September 2022
  • August 2022

Recent Posts

  • Better Avoid Spring Boot Lazy-initialization @PostConstruct Trap
  • How To Handle Large Datasets in Spring Boot
  • How to Log HTTP Incoming Requests in Spring Boot
  • How to Reliably Implement Post-Commit Actions in Spring
  • Brief and Easy Explanation of HATEOAS

TAGS

Algorithms Apache POI Backtracking Date Structures Dynamic Programming engineering Graphs Greedy Horror Stories HSSF Java Java New Features Java Principles Java tips real life Sorting Spring Boot Spring Cloud Strings Trees

Footer

Privacy Policy Cookie Policy Terms and Conditions