import java.util.*;
public class Hello {
public static void undoOper(String str) {
List<Character> lis = new ArrayList<>();
char[] arr = str.toCharArray();
for (char ch : arr) {
lis.add(ch);
}
for (int i = 0; i < lis.size(); i++) {
if (lis.get(i) == '^') {
lis.remove(i);
lis.remove(i - 1);
i = i - 2;
}
}
if (lis.size() == 0)
System.out.print("-1");
for (int i = 0; i < lis.size(); i++) {
System.out.print(lis.get(i));
}
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String[] str = new String[N];
for (int i = 0; i < N; i++) {
str[i] = sc.nextLine();
}
for (int i = 0; i < N; i++) {
undoOper(str[i]);
}
}
}
This program is for undo operation The program accept N string values. The character ^ represents undo action to clear the last previous character.
Input:
Hey ^goooo^^glee^
ora^^nge^^^^
Output:
Hey google
-1(since all char gets erased)
My output:
-1
Hey google