2023. 1. 29. 14:45ㆍJava/coding test
Simple Text Editor
Implement a simple text editor. The editor initially contains an empty string, . Perform operations of the following types:
- append(W) - Append string to the end of .
- delete(k) - Delete the last characters of .
- print(k) - Print the character of .
- 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:
- . We append to , so .
- Print the character on a new line. Currently, the character is c.
- Delete the last characters in , so .
- Append to , so .
- Print the character on a new line. Currently, the character is y.
- Undo the last update to , making empty again (i.e., ).
- Undo the next to last update to (the deletion of the last characters), making .
- Print the character on a new line. Currently, the character is a.
간단한 텍스트 에디터를 구현하세요.
에디터는 빈문자열인 S로 초기화 되어있고, Q번 동작을 합니다.
동작의 종류는 총 4가지로 각각 숫자로 행할 수 있습니다.
- append(W): S 문자열 끝에 "W" 문자열 추가
- delete(k): S 문자열의 끝에서 k개 문자 제거
- print(k): k번째 인덱스에 있는 문자 출력
- undo(): (undo, print를 제외한) 맨마지막에 실행했던 행위로 되돌리기
맨 첫줄은 operation 수를 담는 Q
그 후엔 각각 동작에 대한 입력
undo를 구현하기 위해 stack을 사용하면 됩니다.
public class SimpleTextEditorExample { private static final Scanner scanner = new Scanner(System.in); private static final Stack<String> stack = new Stack<>(); public static void main(String[] args) { int Q = scanner.nextInt(); stack.push(""); while (Q > 0) { int ops = scanner.nextInt(); switch (ops) { case 1: append(scanner.next()); break; case 2: delete(scanner.nextInt()); break; case 3: print(scanner.nextInt()); break; default: undo(); } Q--; } } static void append(String W) { stack.push(stack.peek() + W); } static void delete(int k) { String newStr = stack.peek(); newStr = newStr.substring(0, newStr.length() - k); stack.push(newStr); } static void print(int k) { System.out.println(stack.peek().charAt(k - 1)); } static void undo() { stack.pop(); } }
append할 때, stack에 이전 문자열에 W를 더한 것을 넣어주면 됩니다.
charAt은 i번째 인덱스에 있는 문자를 리턴하는 함수입니다.
k는 1부터 시작되기 떄문에 1을 빼줘야 합니다.
Q가 최대 번 행해질 수 있기 때문에 불필요한 연산이 많을 경우, timeout에러가 발생되어,
peek연산 이전에 stack이 empty가 아닌지 체크하는 부분은 제외했습니다.
'Java > coding test' 카테고리의 다른 글
[Codility - Java] 1. Iterations - 1. Binary Gap (0) | 2023.01.31 |
---|---|
[HackerRank - Java] Day 4 - 1. Grid Challenge (0) | 2023.01.29 |
[HackerRank - Java] Day 4 - 2. Recursive Digit Sum (0) | 2023.01.28 |
[HackerRank - Java] Day 3 - 2. Tower Breakers (0) | 2023.01.28 |
[HackerRank - Java] Day 2 - 3. Counting Sort 1 (0) | 2023.01.27 |