{x}
blog image

Reformat The String

You are given an alphanumeric string s. (Alphanumeric string is a string consisting of lowercase English letters and digits).

You have to find a permutation of the string where no letter is followed by another letter and no digit is followed by another digit. That is, no two adjacent characters have the same type.

Return the reformatted string or return an empty string if it is impossible to reformat the string.

 

Example 1:

Input: s = "a0b1c2"
Output: "0a1b2c"
Explanation: No two adjacent characters have the same type in "0a1b2c". "a0b1c2", "0a1b2c", "0c2a1b" are also valid permutations.

Example 2:

Input: s = "leetcode"
Output: ""
Explanation: "leetcode" has only characters so we cannot separate them by digits.

Example 3:

Input: s = "1229857369"
Output: ""
Explanation: "1229857369" has only digits so we cannot separate them by characters.

 

Constraints:

  • 1 <= s.length <= 500
  • s consists of only lowercase English letters and/or digits.

Solution Explanation

This problem asks to reformat an alphanumeric string such that no two adjacent characters are of the same type (letter or digit). The solution involves these steps:

  1. Separate Characters and Digits: The input string s is processed to separate characters (letters) and digits into two separate lists or strings ( a and b in the code).

  2. Check for Feasibility: The absolute difference between the lengths of the character and digit lists is checked. If this difference is greater than 1, it's impossible to create the desired reformatting, so an empty string is returned.

  3. Construct the Reformatted String: The algorithm iteratively adds elements from the longer list and the shorter list alternately to build the output string. This ensures that no two adjacent characters are of the same type. If one list is longer than the other by one element, that extra element is appended at the end.

Time and Space Complexity Analysis

Time Complexity: O(N), where N is the length of the input string s. This is because the string is iterated through once for separation, and then again (at most) for creating the output string. All other operations are constant-time.

Space Complexity: O(N) in the worst case. This is because, in the worst case, the separated character and digit lists could each be of size approximately N/2. The space used by the output string is also proportional to N.

Code Explanation (Python Example)

The Python code provided implements the solution efficiently:

class Solution:
    def reformat(self, s: str) -> str:
        a = [c for c in s if c.islower()] #List comprehension for characters
        b = [c for c in s if c.isdigit()] #List comprehension for digits
        if abs(len(a) - len(b)) > 1: #Check for feasibility
            return ''
        if len(a) < len(b): #Ensure 'a' is the longer list or equal in length.
            a, b = b, a
        ans = []
        for x, y in zip(a, b): #Interleave elements from both lists.
            ans.append(x + y)
        if len(a) > len(b): #Append remaining element if 'a' is longer.
            ans.append(a[-1])
        return ''.join(ans) #Join the list of strings into a single string.
 

The other languages (Java, C++, Go) follow a very similar approach, demonstrating the same algorithm with only syntactic differences based on the respective language features. They all achieve the same time and space complexity.