Wednesday, May 31, 2023
HomeSoftware EngineeringString Permutations in Java | Learn to Grasp Software program Engineering, DevOps...

String Permutations in Java | Learn to Grasp Software program Engineering, DevOps and Cloud


The problem

On this problem, you must create all permutations of an enter string and take away duplicates if current. This implies you must shuffle all letters from the enter in all doable orders.

Examples:

Permutations.singlePermutations("a") // ["a"]
Permutations.singlePermutations("ab") // ["ab", "ba"]
Permutations.singlePermutations("aabb") // ["aabb","abab","abba","baab","baba","bbaa"]

The order of the permutations doesn’t matter.

The answer in Java code

Possibility 1:

import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;

import java.util.Checklist;

class Permutations {

  public static Checklist<String> singlePermutations(closing String s) {
    return permute("", s);
  }

  personal static Checklist<String> permute(closing String prefix, closing String s) {
  
    return s.isEmpty()
        ? singletonList(prefix)
        : s.chars()
            .distinct()
            .mapToObj(i -> (char) i)
            .map(c -> permute(prefix + c, takeOut(s, c)))
            .flatMap(Checklist::stream)
            .accumulate(toList());
  }

  static String takeOut(closing String s, closing char c) {
    closing int i = s.indexOf(c);
    return s.substring(0, i) + s.substring(i + 1);
  }
}

Possibility 2:

import java.util.*;

class Permutations {
    
    public static Checklist<String> singlePermutations(String s) {
        return permutate("",s,new ArrayList<String>());
    }
    public static Checklist<String> permutate(String permute, String s, Checklist<String> listed) {
      if (s.isEmpty() &&  !listed.comprises(permute + s)) {
        listed.add(permute + s);
      } else {
        for (int i = 0; i < s.size(); i++) {
          permutate(permute + s.charAt(i), s.substring(0,i) + s.substring(i+1,s.size()), listed);
        }
      }
      return listed;
    }
}

Possibility 3:

import java.util.ArrayList;
import java.util.Checklist;
import java.util.HashSet;
class Permutations {
   static void fill(String s,String output,HashSet<String> h) {
    if(s.size()==0) h.add(output);
    for(int i=0;i<s.size();i++){
       char c= s.charAt(i);
       String ns=s.substring(0, i)+s.substring(i+1);
       fill(ns, output+c, h);
    }
  }
    public static Checklist<String> singlePermutations(String s){
      HashSet<String> h = new HashSet<String>();
      fill(s,"",h);
      return new ArrayList<String>(h);
    }
  }

Check circumstances to validate our answer

import org.junit.Check;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.util.*;
import java.util.stream.*;


public class SolutionTest {

    @Check public void example1() {
        assertEquals( new ArrayList<String>(Arrays.asList("a")),
                      Permutations.singlePermutations("a").stream().sorted().accumulate(Collectors.toList()) );
    }
    
    @Check public void example2() {
        assertEquals( new ArrayList<String>(Arrays.asList("ab","ba")),
                      Permutations.singlePermutations("ab").stream().sorted().accumulate(Collectors.toList()) );
    }
    
    @Check public void example3() {
        assertEquals( new ArrayList<String>(Arrays.asList("aabb", "abab", "abba", "baab", "baba", "bbaa")),
                      Permutations.singlePermutations("aabb").stream().sorted().accumulate(Collectors.toList()) );
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments