Skip to main content
added 12 characters in body
Source Link
h.j.k.
  • 19.4k
  • 3
  • 37
  • 93

I know the runtime for generating all permutations is supposed to be O(n!)\$O(n!)\$. I'm not sure if the code I wrote for it runs in O(n!)\$O(n!)\$ though. I'm having trouble analyzing its runtime.

The for(char c: cArray) part is in O(n)\$O(n)\$. What about for (String s: currResults) and for (int i = 0; i < s.length() + 1; i++)?

public static Set<String> getPermutations(String str) {
        char[] cArray = str.toCharArray();
        Set<String> realRes = new HashSet<String>();
        Set<String> results = new HashSet<String>();
        for (char c : cArray) {
            Set<String> currResults = new HashSet<String>(results);
            String charToAdd = String.valueOf(c);
            results.add(charToAdd);
            for (String s : currResults) {
                for (int i = 0; i < s.length() + 1; i++) {
                    String newWord = s.substring(0, i) + charToAdd + s.substring(i);
                    if (newWord.length() == str.length()) {
                        realRes.add(newWord);
                    }
                    else {
                        results.add(newWord);
                    }
                }
            }
        }
        return realRes;
    }

I know the runtime for generating all permutations is supposed to be O(n!). I'm not sure if the code I wrote for it runs in O(n!) though. I'm having trouble analyzing its runtime.

The for(char c: cArray) part is in O(n). What about for (String s: currResults) and for (int i = 0; i < s.length() + 1; i++)?

public static Set<String> getPermutations(String str) {
        char[] cArray = str.toCharArray();
        Set<String> realRes = new HashSet<String>();
        Set<String> results = new HashSet<String>();
        for (char c : cArray) {
            Set<String> currResults = new HashSet<String>(results);
            String charToAdd = String.valueOf(c);
            results.add(charToAdd);
            for (String s : currResults) {
                for (int i = 0; i < s.length() + 1; i++) {
                    String newWord = s.substring(0, i) + charToAdd + s.substring(i);
                    if (newWord.length() == str.length()) {
                        realRes.add(newWord);
                    }
                    else {
                        results.add(newWord);
                    }
                }
            }
        }
        return realRes;
    }

I know the runtime for generating all permutations is supposed to be \$O(n!)\$. I'm not sure if the code I wrote for it runs in \$O(n!)\$ though. I'm having trouble analyzing its runtime.

The for(char c: cArray) part is in \$O(n)\$. What about for (String s: currResults) and for (int i = 0; i < s.length() + 1; i++)?

public static Set<String> getPermutations(String str) {
        char[] cArray = str.toCharArray();
        Set<String> realRes = new HashSet<String>();
        Set<String> results = new HashSet<String>();
        for (char c : cArray) {
            Set<String> currResults = new HashSet<String>(results);
            String charToAdd = String.valueOf(c);
            results.add(charToAdd);
            for (String s : currResults) {
                for (int i = 0; i < s.length() + 1; i++) {
                    String newWord = s.substring(0, i) + charToAdd + s.substring(i);
                    if (newWord.length() == str.length()) {
                        realRes.add(newWord);
                    }
                    else {
                        results.add(newWord);
                    }
                }
            }
        }
        return realRes;
    }
deleted 35 characters in body; edited tags; edited title; edited tags
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Iterative solution for generating all permutations of a string (Java)

I know the runtime for generating all permutations is supposed to be O(n!). I'm not sure if the code I wrote for it runs in O(n!) though. I'm having trouble analyzing its runtime.

The for(char c: cArray) part is in O(n). What about for (String s: currResults) and for (int i = 0; i < s.length() + 1; i++)?

The code is as follows:

public static Set<String> getPermutations(String str) {
        char[] cArray = str.toCharArray();
        Set<String> realRes = new HashSet<String>();
        Set<String> results = new HashSet<String>();
        for (char c : cArray) {
            Set<String> currResults = new HashSet<String>(results);
            String charToAdd = String.valueOf(c);
            results.add(charToAdd);
            for (String s : currResults) {
                for (int i = 0; i < s.length() + 1; i++) {
                    String newWord = s.substring(0, i) + charToAdd + s.substring(i);
                    if (newWord.length() == str.length()) {
                        realRes.add(newWord);
                    }
                    else {
                        results.add(newWord);
                    }
                }
            }
        }
        return realRes;
    }

Thank you!

Iterative solution for generating all permutations of a string (Java)

I know the runtime for generating all permutations is supposed O(n!). I'm not sure if the code I wrote for it runs in O(n!) though. I'm having trouble analyzing its runtime.

The for(char c: cArray) part is in O(n). What about for (String s: currResults) and for (int i = 0; i < s.length() + 1; i++)?

The code is as follows:

public static Set<String> getPermutations(String str) {
        char[] cArray = str.toCharArray();
        Set<String> realRes = new HashSet<String>();
        Set<String> results = new HashSet<String>();
        for (char c : cArray) {
            Set<String> currResults = new HashSet<String>(results);
            String charToAdd = String.valueOf(c);
            results.add(charToAdd);
            for (String s : currResults) {
                for (int i = 0; i < s.length() + 1; i++) {
                    String newWord = s.substring(0, i) + charToAdd + s.substring(i);
                    if (newWord.length() == str.length()) {
                        realRes.add(newWord);
                    }
                    else {
                        results.add(newWord);
                    }
                }
            }
        }
        return realRes;
    }

Thank you!

Iterative solution for generating all permutations of a string

I know the runtime for generating all permutations is supposed to be O(n!). I'm not sure if the code I wrote for it runs in O(n!) though. I'm having trouble analyzing its runtime.

The for(char c: cArray) part is in O(n). What about for (String s: currResults) and for (int i = 0; i < s.length() + 1; i++)?

public static Set<String> getPermutations(String str) {
        char[] cArray = str.toCharArray();
        Set<String> realRes = new HashSet<String>();
        Set<String> results = new HashSet<String>();
        for (char c : cArray) {
            Set<String> currResults = new HashSet<String>(results);
            String charToAdd = String.valueOf(c);
            results.add(charToAdd);
            for (String s : currResults) {
                for (int i = 0; i < s.length() + 1; i++) {
                    String newWord = s.substring(0, i) + charToAdd + s.substring(i);
                    if (newWord.length() == str.length()) {
                        realRes.add(newWord);
                    }
                    else {
                        results.add(newWord);
                    }
                }
            }
        }
        return realRes;
    }
Source Link
fluffychaos
  • 331
  • 2
  • 10

Iterative solution for generating all permutations of a string (Java)

I know the runtime for generating all permutations is supposed O(n!). I'm not sure if the code I wrote for it runs in O(n!) though. I'm having trouble analyzing its runtime.

The for(char c: cArray) part is in O(n). What about for (String s: currResults) and for (int i = 0; i < s.length() + 1; i++)?

The code is as follows:

public static Set<String> getPermutations(String str) {
        char[] cArray = str.toCharArray();
        Set<String> realRes = new HashSet<String>();
        Set<String> results = new HashSet<String>();
        for (char c : cArray) {
            Set<String> currResults = new HashSet<String>(results);
            String charToAdd = String.valueOf(c);
            results.add(charToAdd);
            for (String s : currResults) {
                for (int i = 0; i < s.length() + 1; i++) {
                    String newWord = s.substring(0, i) + charToAdd + s.substring(i);
                    if (newWord.length() == str.length()) {
                        realRes.add(newWord);
                    }
                    else {
                        results.add(newWord);
                    }
                }
            }
        }
        return realRes;
    }

Thank you!