LeetCode 1417: Reformat The String Solution

Master LeetCode problem 1417 (Reformat The String), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1417. Reformat The String

Easy

Problem Explanation

Explanation

To solve this problem, we need to reformat the string such that no two adjacent characters have the same type (letter or digit). We can achieve this by counting the number of letters and digits in the input string s. If the absolute difference between the counts of letters and digits is not greater than 1, we can proceed to construct the reformatted string by interleaving letters and digits. Otherwise, it is impossible to reformat the string.

Algorithm:

  1. Count the number of letters and digits in the input string s.
  2. Check if the absolute difference between the counts of letters and digits is more than 1. If so, return an empty string.
  3. Initialize two pointers, one for letters and the other for digits, to construct the reformatted string.
  4. Based on which count is higher, start constructing the reformatted string by interleaving letters and digits.
  5. Return the reformatted string.

Time Complexity: O(n), where n is the length of the input string s. Space Complexity: O(n)

Solution Code

class Solution {
    public String reformat(String s) {
        int n = s.length();
        StringBuilder sb = new StringBuilder();
        
        int letterCount = 0, digitCount = 0;
        for (char c : s.toCharArray()) {
            if (Character.isLetter(c))
                letterCount++;
            else
                digitCount++;
        }
        
        if (Math.abs(letterCount - digitCount) > 1)
            return "";
        
        int i = 0, j = 0;
        if (letterCount >= digitCount) {
            i = 0; j = 1;
        } else {
            i = 1; j = 0;
        }
        
        for (char c : s.toCharArray()) {
            if (Character.isLetter(c)) {
                sb.insert(i, c);
                i += 2;
            } else {
                sb.insert(j, c);
                j += 2;
            }
        }
        
        return sb.toString();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1417 (Reformat The String)?

This page provides optimized solutions for LeetCode problem 1417 (Reformat The String) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1417 (Reformat The String)?

The time complexity for LeetCode 1417 (Reformat The String) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1417 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1417 in Java, C++, or Python.

Back to LeetCode Solutions