Frontend School

Simulate Simple Text Editor Output

DSA
easy

Simulate Simple Text Editor Output

Easy
Stack
String
Simulation

Overview

At Myntra, we process vast amounts of data, including user-generated content and internal logs. Often, this data goes through various transformations and cleaning steps. Imagine a simplified text processing utility that takes a sequence of commands and produces a final string output. This problem simulates a basic text editor's behavior, focusing on character insertion and an "undo" operation, which is a fundamental concept in many software systems, including our internal tools for content moderation and data curation.

What you need to do / Task

You are tasked with implementing a function that simulates the output of a very basic text editor. The editor processes a string of commands, character by character.

  1. If the character is a lowercase English letter, it is appended to the current text buffer.
  2. If the character is a special "undo" command (^), the last character appended to the buffer is removed. If the buffer is empty, the undo command has no effect.

Your function should return the final string present in the text buffer after all commands have been processed.

Input & output

Function Signature: function simulateEditorOutput(commands: string): string

Input:

  • commands: A string consisting of lowercase English letters ('a'-'z') and the undo command character ('^').

Output:

  • A string representing the final content of the editor's buffer after processing all commands.

Clarifications

  • The input commands string will only contain lowercase English letters and the ^ character.
  • The undo operation (^) only removes the immediately preceding character that was added to the buffer.
  • If an undo operation is encountered when the buffer is empty, it should simply be ignored and have no effect.
  • The final output string can be empty if all characters are undone or if the input was empty.

Examples

1

Example 1

Input:

commands = "abc^de"

Output:

"abde"

Note:

  1. 'a' is added: buffer = "a"
  2. 'b' is added: buffer = "ab"
  3. 'c' is added: buffer = "abc"
  4. '^' (undo): 'c' is removed: buffer = "ab"
  5. 'd' is added: buffer = "abd"
  6. 'e' is added: buffer = "abde" Final buffer: "abde"
2

Example 2

Input:

commands = "ab^^c"

Output:

"c"

Note:

  1. 'a' is added: buffer = "a"
  2. 'b' is added: buffer = "ab"
  3. '^' (undo): 'b' is removed: buffer = "a"
  4. '^' (undo): 'a' is removed: buffer = ""
  5. 'c' is added: buffer = "c" Final buffer: "c"
3

Example 3

Input:

commands = "^a^b^"

Output:

""

Note:

  1. '^' (undo): buffer is empty, no effect: buffer = ""
  2. 'a' is added: buffer = "a"
  3. '^' (undo): 'a' is removed: buffer = ""
  4. 'b' is added: buffer = "b"
  5. '^' (undo): 'b' is removed: buffer = "" Final buffer: ""

Constraints

Constraints & assumptions

  • 1 <= commands.length <= 10^5
  • commands will only contain lowercase English letters ('a'-'z') and the undo character ('^').

What interviewers evaluate

  • Correct application of stack data structure principles to manage character additions and removals.
  • Robust handling of edge cases, particularly when an undo operation is applied to an empty buffer.
  • Efficiency of the solution in terms of time and space complexity.
  • Readability and maintainability of the code.

Loading editor...