Remove Duplicate Letters

To solve this coding challenge, you need to remove duplicate letters from a given string
s
such that every letter appears only once and the resultant string is the smallest possible in lexicographical order. Let's dissect and understand the steps thoroughly.

Explanation

Problem Breakdown:

The essence of the problem lies in maintaining a sequence of characters where:
  1. Each character appears only once.
  2. The resultant sequence is the smallest possible when compared dictionary-wise to any other possible combinations.
  3. Constraints and Considerations:

  4. Input consists solely of lowercase English letters.
  5. Ensure all input constraints are considered, especially the maximum length of the string which is 10,000.
  6. Strategy:

    The approach leverages a stack to build the result string while ensuring the conditions above are met. The stack helps maintain the order of the characters, and we utilize additional data structures to manage counts and checks:
  7. A counter to track the remaining occurrences of each character in the string.
  8. A set to keep track of characters already present in the resultant stack to avoid duplicates.
  9. Process Flow:
  10. Iterate through each character in the string.
  11. Decrement the count of the current character in the counter as it gets processed.
  12. If the character is already added to the stack (checked via a set), continue to the next character.
  13. Maintain the stack such that the characters are in the smallest lexicographical order by comparing and potentially removing elements in the stack based on the remaining counts.
  14. Append the current character to the stack and mark it as visited.
  15. Construct the result string by joining the elements of the stack.
  16. Detailed Steps in Pseudocode:

  17. Initialize a stack to keep the resultant characters.
  18. Initialize a counter to count character occurrences in the input string.
  19. Initialize a set to keep track of characters currently in the stack.
  20. Loop through each character in the input string:
    • Decrement the character count in the counter.
    • If the character is already visited, skip to the next character.
    • While the current character is smaller than the character at the top of the stack and the top character appears later in the string too, pop the stack.
      • Remove the popped character from the visited set.
    • Add the current character to the stack and mark it as visited.
  21. Return the joined stack as the resultant string.
Pseudocode:

Main function to remove duplicate letters and ensure the smallest lexicographical order

                                            
# Function definition to remove duplicate letters while maintaining lexicographical order
Function removeDuplicateLetters(string input_string):
    Initialize stack as an empty list # Stack to build the result
    Initialize counter as a dictionary with character counts of input_string # Track remaining characters
    Initialize visited as an empty set # Track characters in the stack
    
    # Loop through each character in the string
    For each character in input_string:
        Decrement counter[character] by 1 # One less occurrence left
        
        # Skip if character is already in the stack
        If character in visited:
            Continue to the next iteration
        
        # Maintain lexicographical order by comparison and popping
        While stack is not empty AND character < top of stack AND counter[top of stack] > 0:
            Pop the top of stack
            Remove the popped character from visited
            
        # Append current character to stack and mark as visited
        Append character to stack
        Add character to visited
    
    # Return the stack as a joined string
    Return join the stack to form the resultant string

                                        

Detailed Steps:

  • Initialize necessary structures (stack, counter, set).
  • Loop through each character, adjust the character count.
  • Ensure characters already visited are skipped.
  • Utilize the stack for maintaining the lexicographical order by selectively removing characters based on comparisons and remaining counts.
  • Finally, return the constructed string by joining elements in the stack.
This method ensures the smallest lexicographical sequence where each letter appears once. By leveraging data structures efficiently, we achieve an optimal solution, satisfying the problem constraints effectively.