Simple Text Editor

thanksgiving_dayHope you had a nice Thanksgiving Day and today you are enjoying Black Friday shopping.

Logged on to HackerRank and selected the Simple Text Editor challenge. To get a description please visit their site at: https://www.hackerrank.com/challenges/simple-text-editor

The idea is to perform four operations on a string. The operation black_fridayof interest is undoing. The operation is defined as follows: undo() – Undo the last (not previously undone) operation of type 1 (append) or 2 (delete), reverting to the state it was in prior to that operation.

To me this challenge appears to call for a stack. Using the sample data the output of the Eclipse IDE (yesterday updated to: Eclipse Java EE IDE for Web Developers Version: Neon.1a Release (4.6.1) Build id: 20161007-1200) console follows:

8

1 abc

3 3

2 3

1 xy

3 2

4

4

3 1

c

y

a

My complete solution in Java follows:

package john.canessa.simple.text.editor;

import java.util.Scanner;

import java.util.Stack;

class TextEditor {

StringBuilder sb     = null;

Stack<String> stack  = null;

/*

* constructor

*/

public TextEditor() {

sb     = new StringBuilder();

stack = new Stack<String>();

}

/*

* append string

*/

public String append(String w) {

              stack.push(sb.toString());

sb.append(w);

return sb.toString();

}

/*

* delete last k characters

*/

public String delete(int k) {

              stack.push(sb.toString());

sb.setLength(sb.length() – k);

return sb.toString();

}

/*

* print the character at k

*/

public void print(int k) {

System.out.println(sb.charAt(k – 1));

}

/*

* undo the last 1 or 2 operation

*/

public String undo() {

              if (!stack.isEmpty()) {

                     sb = new StringBuilder(stack.pop());

              }

return sb.toString();

}

/*

* show the string (for testing only)

*/

public void show() {

System.out.println(“show <<< bs ==>” + sb.toString() + “<==”);

}

}

public class Solution {

public static void main(String[] args) {

TextEditor editor    = new TextEditor();

Int k               = -1;

// **** open scanner ****

Scanner sc = new Scanner(System.in);

// **** ****

int Q = sc.nextInt();

// **** perform operations ****

for (int q = 0; q < Q; q++) {

int op = sc.nextInt();

switch (op) {

case 1:              // append string to S

String w = sc.next();

editor.append(w);

break;

case 2:              // delete last k characters

k = sc.nextInt();

editor.delete(k);

break;

case 3:              // print k th character in S

k = sc.nextInt();

editor.print(k);

break;

case 4:              // undo the last operation of type 1 or 2

editor.undo();

break;

default:

System.err.println(“main <<< unexpected op: ” + op);

System.exit(-1);

break;

}

//                   // **** display string ****

//                  

//                   editor.show();

}

// **** close scanner ****

sc.close();

}

}

stack_diagramThe constructor allocates an empty stack to allow us to undo changes. On methods append() and delete() the current content of the string builder is pushed as a string before any changes are made. When the undo() method is called, we only need to pop() the stack into a new string builder.

I fully understand that some additional checks might have been nice, but the challenge contains the following statement: “It is guaranteed that the sequence of operations given as input is possible to perform”.

If you have comments or questions regarding this post or any other in this blog, please do not hesitate and contact me via email.

John

john.canessa@gmail.com

Follow me on Twitter: @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.