daniel_lawson9999

combinations

May 15th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. public class test {
  2.     public static void main(String[] args) {
  3.         HashSet<String> stuff =combinations("Daniel",6,6,new HashSet<String>());
  4.         System.out.println(stuff);
  5.         System.out.println("length: " +stuff.size());
  6.     }
  7.     public static HashSet<String> combinations(String s, int k,int count, HashSet<String> set){
  8.         s=s.toLowerCase();
  9.         if(s.length() > k){
  10.             Set<Character> letters = new HashSet<>();
  11.             for (int i = 0; i < s.length(); i++) {
  12.                 if (letters.contains(s.charAt(i))) {
  13.                     s = s.substring(0, i) + s.substring(i + 1);
  14.                     i--;
  15.                     System.out.println(s);
  16.                 } else {
  17.                     letters.add(s.charAt(i));
  18.                 }
  19.             }
  20.             for(int i = 0; i <s.length();i++){
  21.                 String temp = s;
  22.                 set.addAll(combinations(temp.substring(0,i) + temp.substring(i+1),k,count,set));
  23.             }
  24.             return set;
  25.         }
  26.         else if(count > 1){
  27.             for(int i = 0; i < s.length();i++){
  28.                 set.addAll(combinations(s.charAt(i) + s.substring(0,i) + s.substring(i+1),k,count-1,set));
  29.             }
  30.             return set;
  31.  
  32.         }
  33.         else{
  34.             set.add(s);
  35.             return set;
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment