We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home
  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Interview Preparation Kits
  3. 3 Months Preparation Kit
  4. Week 9
  5. Simple Text Editor

Simple Text Editor

Problem
Submissions
Leaderboard
Discussions
Editorial
HackerRank Logo
|
  1. Prepare
  2. Interview Preparation Kits
  3. 3 Months Preparation Kit
  4. Week 9
  5. Simple Text Editor
Exit Full Screen View
  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

Implement a simple text editor. The editor initially contains an empty string, . Perform operations of the following types:

  1. append - Append string to the end of .
  2. delete - Delete the last characters of .
  3. print - Print the character of .
  4. undo - Undo the last (not previously undone) operation of type or , reverting to the state it was in prior to that operation.

Example


operation
index   S       ops[index]  explanation
-----   ------  ----------  -----------
0       abcde   1 fg        append fg
1       abcdefg 3 6         print the 6th letter - f
2       abcdefg 2 5         delete the last 5 letters
3       ab      4           undo the last operation, index 2
4       abcdefg 3 7         print the 7th characgter - g
5       abcdefg 4           undo the last operation, index 0
6       abcde   3 4         print the 4th character - d

The results should be printed as:

f
g
d

Input Format

The first line contains an integer, , denoting the number of operations.
Each line of the subsequent lines (where ) defines an operation to be performed. Each operation starts with a single integer, (where ), denoting a type of operation as defined in the Problem Statement above. If the operation requires an argument, is followed by its space-separated argument. For example, if and , line will be 1 abcd.

Constraints

  • The sum of the lengths of all in the input .
  • The sum of over all delete operations .
  • All input characters are lowercase English letters.
  • It is guaranteed that the sequence of operations given as input is possible to perform.

Output Format

Each operation of type must print the character on a new line.

Sample Input

STDIN   Function
-----   --------
8       Q = 8
1 abc   ops[0] = '1 abc'
3 3     ops[1] = '3 3'
2 3     ...
1 xy
3 2
4 
4 
3 1

Sample Output

c
y
a

Explanation

Initially, is empty. The following sequence of operations are described below:

  1. . We append to , so .
  2. Print the character on a new line. Currently, the character is c.
  3. Delete the last characters in (), so .
  4. Append to , so .
  5. Print the character on a new line. Currently, the character is y.
  6. Undo the last update to , making empty again (i.e., ).
  7. Undo the next to last update to (the deletion of the last characters), making .
  8. Print the character on a new line. Currently, the character is a.
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Helpdesk
  • Careers
  • Terms Of Service
  • Privacy Policy

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website