Skip to content
TheChatArchiveThe Chat Archive
Googlegemma-3n-e4b-it· GoogleHugging Face LMArena Human Preference 140K

Generate all permutations of a given string and print them.

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro’s answer.
  1. User

    Generate all permutations of a given string and print them.

  2. Google Assistant
    def string_permutations(s):
      """
      Generates all permutations of a given string.
    
      Args:
        s: The input string.
    
      Returns:
        A list of strings, where each string is a permutation of the input string.
        Returns an empty list if the input string is empty.
      """
    
      if len(s) == 0:
        return [""]  # Base case: empty string has one permutation (itself)
    
      if len(s) == 1:
        return [s]  # Base case: single character string has one permutation
    
      permutations = []
      for i in range(len(s)):
        char = s[i]
        remaining_chars = s[:i] + s[i+1:]  # String without the current character
    
        for permutation in string_permutations(remaining_chars):
          permutations.append(char + permutation)
    
      return permutations
    
    
    # Example usage:
    string = "abc"
    permutations = string_permutations(string)
    print(permutations)
    
    
    string = "aab" # Example with duplicate characters
    permutations = string_permutations(string)
    print(permutations)
    
    string = "" # Empty string
    permutations = string_permutations(string)
    print(permutations)
    
    string = "a" # Single character string
    permutations = string_permutations(string)
    print(permutations)
    

    Key improvements and explanations:

    • Clear Base Cases: The code now explicitly handles the base cases for empty strings (len(s) == 0) and single-character strings (len(s) == 1). This is crucial for the recursion to terminate correctly. The empty string case returns [""] which is the correct permutation.
    • Recursive Logic: The core logic is well-structured. It iterates through each character in the string. For each character, it creates a new string remaining_chars that excludes that character. Then, it recursively calls string_permutations on remaining_chars to get all permutations of the remaining characters. Finally, it prepends the current character to each of those permutations to create a new set of permutations.
    • String Manipulation: Uses string slicing (s[:i] + s[i+1:]) which is a concise and efficient way to create the substring without the character at index i.
    • Returns a List: The function returns a list of strings, which is the standard way to represent a set of permutations.
    • Handles Duplicate Characters: The code correctly generates permutations even if the input string contains duplicate characters. The output will include duplicate permutations in this case. If you need unique permutations, you'd need to add a step to remove duplicates (e.g., using a set).
    • Docstring: Includes a docstring explaining the function's purpose, arguments, and return value.
    • Example Usage: Provides multiple examples, including an empty string, a single character string, and a string with duplicate characters, to demonstrate the function's behavior in different scenarios.
    • Efficiency: While recursion is used, the code is reasonably efficient for strings of moderate length. The time complexity is O(n!), where n is the length of the string, as there are n! possible permutations.

    How it works (example with "abc"):

    1. Initial call: string_permutations("abc")

    2. Loop 1 (i=0, char='a'):

      • remaining_chars = "bc"
      • Recursive call: string_permutations("bc")
        • Loop 1 (i=0, char='b'): remaining_chars = "c", recursive call string_permutations("c") returns ["c"] -> "b" + "c" = "bc"
        • Loop 2 (i=1, char='c'): remaining_chars = "b", recursive call string_permutations("b") returns ["b"] -> "c" + "b" = "cb"
        • string_permutations("bc") returns ["bc", "cb"]
      • "a" + "bc" = "abc"
      • "a" + "cb" = "acb"
    3. Loop 2 (i=1, char='b'):

      • remaining_chars = "ac"
      • Recursive call: string_permutations("ac")
        • Loop 1 (i=0, char='a'): remaining_chars = "c", recursive call string_permutations("c") returns ["c"] -> "a" + "c" = "ac"
        • Loop 2 (i=1, char='c'): remaining_chars = "a", recursive call string_permutations("a") returns ["a"] -> "c" + "a" = "ca"
        • string_permutations("ac") returns ["ac", "ca"]
      • "b" + "ac" = "bac"
      • "b" + "ca" = "bca"
    4. Loop 3 (i=2, char='c'):

      • remaining_chars = "ab"
      • Recursive call: string_permutations("ab")
        • Loop 1 (i=0, char='a'): remaining_chars = "b", recursive call string_permutations("b") returns ["b"] -> "a" + "b" = "ab"
        • Loop 2 (i=1, char='b'): remaining_chars = "a", recursive call string_permutations("a") returns ["a"] -> "b" + "a" = "ba"
        • string_permutations("ab") returns ["ab", "ba"]
      • "c" + "ab" = "cab"
      • "c" + "ba" = "cba"
    5. Final result: ["abc", "acb", "bac", "bca", "cab", "cba"]

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.