Find All Permutation of a given String in Easy Way

import java.util.HashSet;

public class StringPermutation {

public static void main(String[] args){
HashSet<String> output = new HashSet<>();
String input = "ABC";
doPermutation(input, output);

private static void doPermutation(String input, HashSet<String> set) {
HashSet<String> inputList = new HashSet<>();
for(int i=0; i<input.length()-1; i++){
int finalI = i;
inputList.forEach((k) -> {
for (int j = finalI; j < input.length(); j++) {
set.add(swap(k, finalI, j));
inputList = (HashSet<String>) set.clone();

private static String swap(String str, int firstIndex, int secondIndex) {
StringBuilder sb = new StringBuilder(str);
sb.setCharAt(firstIndex, str.charAt(secondIndex));
sb.setCharAt(secondIndex, str.charAt(firstIndex));
return sb.toString();




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store