DEV Community

Cover image for 3561. Resulting String After Adjacent Removals
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

3561. Resulting String After Adjacent Removals

3561. Resulting String After Adjacent Removals

Difficulty: Medium

Topics: Senior, String, Stack, Simulation, Weekly Contest 451

You are given a string s consisting of lowercase English letters.

You must repeatedly perform the following operation while the string s has at least two consecutive characters:

  • Remove the leftmost pair of adjacent characters in the string that are consecutive in the alphabet, in either order (e.g., 'a' and 'b', or 'b' and 'a').
  • Shift the remaining characters to the left to fill the gap.

Return the resulting string after no more operations can be performed.

Note: Consider the alphabet as circular, thus 'a' and 'z' are consecutive.

Example 1:

  • Input: s = "abc"
  • Output: "c"
  • Explanation:
    • Remove "ab" from the string, leaving "c" as the remaining string.
    • No further operations are possible. Thus, the resulting string after all possible removals is "c".

Example 2:

  • Input: s = "adcb"
  • Output: ""
  • Explanation:
    • Remove "dc" from the string, leaving "ab" as the remaining string.
    • Remove "ab" from the string, leaving "" as the remaining string.
    • No further operations are possible. Thus, the resulting string after all possible removals is "".

Example 3:

  • Input: s = "zadb"
  • Output: "db"
  • Explanation:
    • Remove "za" from the string, leaving "db" as the remaining string.
    • No further operations are possible. Thus, the resulting string after all possible removals is "db".

Example 4:

  • Input: s = "a"
  • Output: "a"

Example 5:

  • Input: s = "ab"
  • Output: ""

Example 6:

  • Input: s = "az"
  • Output: ""

Example 7:

  • Input: s = "za"
  • Output: ""

Example 8:

  • Input: s = "zz"
  • Output: "zz"

Example 9:

  • Input: s = "abba"
  • Output: ""

Example 10:

  • Input: s = "azb"
  • Output: "b"

Example 11:

  • Input: s = "cba"
  • Output: "a"

Example 12:

  • Input: s = "ace"
  • Output: "ace"

Constraints:

  • 1 <= s.length <= 10⁵
  • s consists only of lowercase English letters.

Hint:

  1. Traverse the string from left to right and use a stack to perform the removals.

Solution:

We solve the problem by scanning the string from left to right with a stack. For each character, we compare it with the stack top; if the two characters are consecutive in the alphabet in either order, including the circular pair 'a' and 'z', we remove the stack top and skip the current character. Otherwise, we push the current character onto the stack. The final stack contents form the resulting string.

Approach

  • Initialize an empty stack.
  • Traverse each character in s.
  • If the stack is not empty, compare the current character with the stack top.
  • Two characters are removable if:
    • Their ASCII difference is 1, e.g. "ab", "ba".
    • Their ASCII difference is 25, e.g. "az", "za".
  • If removable, pop the stack top and discard the current character.
  • Otherwise, push the current character onto the stack.
  • After processing all characters, join the stack into a string and return it.

Let's implement this solution in PHP: 3561. Resulting String After Adjacent Removals

<?php
/**
 * @param String $s
 * @return String
 */
function resultingString(string $s): string
{
    ...
    ...
    ...
    /**
     * go to ./solution.php
     */
}

// Test cases
echo resultingString("abc") .  "\n";        // Output: "c"
echo resultingString("adcb") .  "\n";       // Output: ""
echo resultingString("zadb") .  "\n";       // Output: "db"
echo resultingString("a") .  "\n";          // Output: "a"
echo resultingString("ab") .  "\n";         // Output: ""
echo resultingString("az") .  "\n";         // Output: ""
echo resultingString("za") .  "\n";         // Output: ""
echo resultingString("zz") .  "\n";         // Output: "zz"
echo resultingString("abba") .  "\n";       // Output: ""
echo resultingString("azb") .  "\n";        // Output: "b"
echo resultingString("cba") .  "\n";        // Output: "a"
echo resultingString("ace") .  "\n";        // Output: "ace"
echo resultingString("zaz") .  "\n";        // Output: "z"
echo resultingString("abcba") .  "\n";      // Output: "a"
echo resultingString("abccba") .  "\n";     // Output: "ca"
?>
Enter fullscreen mode Exit fullscreen mode

Explanation

  • The stack simulates the repeated removal process efficiently.
  • Processing left to right ensures that whenever a removable pair is found, it is handled as early as possible.
  • Because earlier removals are already resolved in the stack, comparing only the current character with the stack top is sufficient.
  • The circular alphabet condition is handled by checking whether the absolute ASCII difference is either 1 or 25.
  • If the difference is 1, the characters are normally consecutive.
  • If the difference is 25, the characters are 'a' and 'z', which are consecutive circularly.
  • Characters that cannot be removed remain in the stack in their original relative order.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of s. Each character is pushed and popped at most once.
  • Space Complexity: O(n) in the worst case, for the stack storing characters that cannot be removed.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
Buy Me A Coffee

If you want more helpful content like this, feel free to follow me:

Top comments (0)