im trying to analysis the complexity of this code . i predicated that its o(n^2) because for loop take a o(n) inside a recursion function thats take o(n) o(n) * o(n) = o(n^2) however im not sure .
public class main
{
static Set<String> setString = new HashSet<>();
public static void main(String[] args)
{
// TODO Auto-generated method stub
main m = new main();
m.permute("sanad", 0);
for(String s : setString)
{
System.out.println(s);
}
}
public void permute(String str , int i )
{
if (i>=str.length())
{
return;
}
for(int j = 0 ; j < str.length();j++)
{
StringBuilder b = new StringBuilder(str. replaceFirst(String.valueOf(str.charAt(i)), ""));
b.insert(j,str.charAt(i));
setString.add(b.toString());
}
permute(str, ++i);
}
}