educative.io

Would this be also an acceptable solution?

    static ArrayList<String> generateCombinations(int n) {
    		return new ArrayList<>(comb(n));
    	}

    	static Set<String> comb(int n) {

    		if(n == 1) {
    			return new HashSet<>(Collections.singleton("{}"));
    		}

    		Set<String> tmp = comb(n-1);
    		Set<String> res = new HashSet<>();
    		for(String s : tmp) {
    			for(int i = 0; i < s.length(); i++) {
    				String pre = s.substring(0, i + 1);
    				res.add(pre + "{}" + s.substring(i+1));
    			}
    		}

    		return res;
    	}


----
Type your question above this line.

Course: https://www.educative.io/collection/5642554087309312/5679846214598656
Lesson: https://www.educative.io/collection/page/5642554087309312/5679846214598656/160001

Hi @Peter_Litvak ,

Yes, it is the acceptable solution :+1: