# Powerset generation using recursion and time complexity

I have a program that generates power set of a given string, the set being the characters of the string.

public static Set subsets(String s) {
Set subsets = new HashSet();
if (s.length() == 0) {
} else if (s.length() == 1) {
} else {
for (int i = 0; i < s.length() - 1; i++) {
Set sets = subsets(s.substring(i + 1));
for (String st : sets) {
}
}
}
return subsets;
}


I don't seem to understand how to calculate time complexity of the above implementation.I understand that String.substring and creating Set Objects are pretty expensive, but if I assume these two operations to take constant time, what would be the overall time complexity?

• Can you please replace your code with pseudocode? I would also check here and here. What have you tried so far to analyze it? What specifically are you having trouble with? – ryan Aug 4 '17 at 21:12
• It appears you have copied your code incorrectly, because of this line: for (int i = 0; i sets = subsets(s.substring(i + 1)); – Riley Aug 5 '17 at 15:19
• Edited. There was a problem in the markup – Kumar Bibek Aug 5 '17 at 21:15
• Possible duplicate of Is there a system behind the magic of algorithm analysis? – David Richerby Aug 5 '17 at 21:31
• Personally, I would take real code over pseudo-code any day of the week. Especially code written in a common language that isn't relying on any uncommon features or subtle details. – Derek Elkins left SE Aug 5 '17 at 21:37

In fact, if your code runs exactly as you describe, namely given a set $S$ as a string it generate all subsets of the symbols of the input string, then the running time is trivial. You just need to count the number of different subsets generated by your algorithm/code. Input size is the length of the input string, i.e., number of characters. So, if the number of characters is $n$ then there are total $2^n$ different subsets. So, the time complexity is $O(2^n)$.