Apply Special String Commands

Quick Overview

Apply letter appends, last-character removal, duplication, and reversal commands to initially empty text. Respect left-to-right processing and return the complete final string, including an empty result.

Apply Special String Commands

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Apply Special String Commands You are given a string `s` containing lowercase English letters and the commands `*`, `#`, and `%`. Start with empty working text and process `s` from left to right: - A lowercase letter appends that letter to the working text. - `*` removes the last character if the working text is nonempty. Otherwise it does nothing. - `#` appends a copy of the entire current working text to itself. - `%` reverses the current working text. Return the complete final text after all input characters have been processed. ### C++ Interface ```cpp class Solution { public: string processStr(string s); }; ``` ### Examples **Example 1** ```text s = "pq#%*r" Output: "qpqr" ``` The working text changes as follows: `"p"`, `"pq"`, `"pqpq"`, `"qpqp"`, `"qpq"`, `"qpqr"`. **Example 2** ```text s = "**t*#%" Output: "" ``` The first two removals do nothing. Appending `t` and removing it leaves empty text; copying and reversing that empty text keep it empty. ### Constraints - `1 <= s.length <= 20`. - Every character is a lowercase English letter or one of `*`, `#`, `%`. ### Clarifying Questions - Does `#` copy the input processed so far? No. It copies the current working text, including all earlier command effects. - Can `*` remove a command symbol from the input? No. It removes only the current working text's last character, if one exists. - Does `%` change the order of the remaining input commands? No. It affects only the text built so far. - Can the returned string be empty? Yes. ### What a Strong Answer Covers - The exact left-to-right meaning of all four kinds of input characters. - Correct behavior of removal, copying, and reversal on empty text. - Preservation of repeated letters and the order produced by earlier commands. - Time and memory bounds that account for repeated duplication, with at most twenty input characters. ### Follow-up Questions - Which of the three commands leave empty working text unchanged? - What happens when two reversal commands occur consecutively without another operation between them? ```hint Follow the commands Maintain the current text explicitly. Check whether it is empty before removing its last character, and duplicate a snapshot of the whole current text when processing #. ```

Overview: Apply letter appends, last-character removal, duplication, and reversal commands to initially empty text. Respect left-to-right processing and return the complete final string, including an empty result.

Amazon logo
Amazon
Apr 30, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Apply Special String Commands

You are given a string s containing lowercase English letters and the commands *, #, and %. Start with empty working text and process s from left to right:

  • A lowercase letter appends that letter to the working text.
  • * removes the last character if the working text is nonempty. Otherwise it does nothing.
  • # appends a copy of the entire current working text to itself.
  • % reverses the current working text.

Return the complete final text after all input characters have been processed.

C++ Interface

class Solution {
public:
    string processStr(string s);
};

Examples

Example 1

s = "pq#%*r"
Output: "qpqr"

The working text changes as follows: "p", "pq", "pqpq", "qpqp", "qpq", "qpqr".

Example 2

s = "**t*#%"
Output: ""

The first two removals do nothing. Appending t and removing it leaves empty text; copying and reversing that empty text keep it empty.

Constraints

  • 1 <= s.length <= 20 .
  • Every character is a lowercase English letter or one of * , # , % .

Clarifying Questions Guidance

  • Does # copy the input processed so far? No. It copies the current working text, including all earlier command effects.
  • Can * remove a command symbol from the input? No. It removes only the current working text's last character, if one exists.
  • Does % change the order of the remaining input commands? No. It affects only the text built so far.
  • Can the returned string be empty? Yes.

What a Strong Answer Covers Guidance

  • The exact left-to-right meaning of all four kinds of input characters.
  • Correct behavior of removal, copying, and reversal on empty text.
  • Preservation of repeated letters and the order produced by earlier commands.
  • Time and memory bounds that account for repeated duplication, with at most twenty input characters.

Follow-up Questions Guidance

  • Which of the three commands leave empty working text unchanged?
  • What happens when two reversal commands occur consecutively without another operation between them?