Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class test {
- public static void main(String[] args) {
- HashSet<String> stuff =combinations("Daniel",6,6,new HashSet<String>());
- System.out.println(stuff);
- System.out.println("length: " +stuff.size());
- }
- public static HashSet<String> combinations(String s, int k,int count, HashSet<String> set){
- s=s.toLowerCase();
- if(s.length() > k){
- Set<Character> letters = new HashSet<>();
- for (int i = 0; i < s.length(); i++) {
- if (letters.contains(s.charAt(i))) {
- s = s.substring(0, i) + s.substring(i + 1);
- i--;
- System.out.println(s);
- } else {
- letters.add(s.charAt(i));
- }
- }
- for(int i = 0; i <s.length();i++){
- String temp = s;
- set.addAll(combinations(temp.substring(0,i) + temp.substring(i+1),k,count,set));
- }
- return set;
- }
- else if(count > 1){
- for(int i = 0; i < s.length();i++){
- set.addAll(combinations(s.charAt(i) + s.substring(0,i) + s.substring(i+1),k,count-1,set));
- }
- return set;
- }
- else{
- set.add(s);
- return set;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment