To solve this problem efficiently, we need to ensure that the remaining digits in the string meet the following criteria:
- Each digit ('1' and '2') appears an even number of times.
- The resulting integer is the largest possible.
Given the constraints (N can be as large as 200,000), we need an algorithm with linear time complexity, O(N). Here's how we can achieve this:
Step-by-Step Reasoning:
-
Count the Occurrences:
- First, count the total number of '1's and '2's in the string.
-
Determine If Removal Is Needed:
- If both counts are even, no removal is needed; the entire string is the answer.
- If the count of '1's is odd, we need to remove one '1'.
- If the count of '2's is odd, we need to remove one '2'.
- If both counts are odd, we need to remove one '1' and one '2'.
-
Determine Which Digits to Remove for Maximum Value:
- Removing '1's: To maximize the integer value, remove the earliest '1' (i.e., the one closest to the start of the string). This ensures that the remaining '1's are towards the end, preserving '2's in higher positions.
- Removing '2's: Conversely, remove the latest '2' (i.e., the one closest to the end of the string). This keeps earlier '2's intact, maximizing the higher positions.
-
Build the Resulting String:
- Iterate through the original string, skipping the indices of the digits marked for removal.
- Concatenate the remaining digits to form the final answer.
Implementation Details:
-
Efficient Removal: Instead of physically removing characters from the string (which can be inefficient for large strings), we'll mark indices to be skipped and then construct the final string by iterating through the original string.
-
Handling Large Inputs: Since N can be up to 200,000, it's crucial to use data structures and operations that are linear in time and space.
Java Implementation:
Below is the Java implementation that adheres to the above reasoning. It includes a Solution class with the required solution method and a Main class with a main method for thorough testing.
Explanation of the Code:
-
Counting the Digits:
- We iterate through the string to count the number of '1's and '2's.
-
Determining Which Digits to Remove:
- If the count of '1's is odd (
remove1), we identify the earliest '1' in the string.
- If the count of '2's is odd (
remove2), we identify the latest '2' in the string.
-
Removing the Identified Digits:
- We use a
StringBuilder to construct the final string.
- While iterating through the original string, we skip the indices marked for removal.
-
Handling Edge Cases:
- All Counts Even: If both counts are even, no removal occurs, and the original string is returned.
- Single Type of Digit: The algorithm gracefully handles cases where only '1's or only '2's are present.
- Both Counts Odd: Both an earliest '1' and latest '2' are removed to satisfy the parity condition.
-
Testing:
- The
main method includes several test cases, including the examples provided and additional edge cases to ensure robustness.
Time and Space Complexity:
- Time Complexity:
O(N), where N is the length of the input string. We traverse the string a constant number of times.
- Space Complexity:
O(N), due to the use of StringBuilder to construct the result.
Conclusion:
This solution efficiently addresses the problem by ensuring that the counts of each digit are even and that the resulting integer is as large as possible. It leverages straightforward counting and selective removal strategies to achieve optimal performance within the given constraints.