You are viewing a single comment's thread. Return to all comments →
U can check my Solution here:
import java.io.; import java.util.;
public class Solution {
public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ LinkedList<String> stack=new LinkedList<String>(); int i,n,type,k; Scanner sc=new Scanner(System.in); n=sc.nextInt(); StringBuilder sb=new StringBuilder(); for(i=0;i<n;i++) { type=sc.nextInt(); //System.out.println("i: "+i+" Type: "+type); if(type==1) { //append String str=sc.next(); sb.append(str); stack.offerFirst(Integer.toString(str.length())); } if(type==2) { //delete k=sc.nextInt(); int len=sb.length(); stack.offerFirst(sb.substring(len-k,len)); String sub=sb.substring(0,len-k); sb.setLength(0); sb.append(sub); } if(type==3) { //print k=sc.nextInt(); System.out.println(sb.charAt(k-1)); } if(type==4) { //undo String top=stack.pollFirst(); if(top.charAt(0)>=97 && top.charAt(0)<=122) { //if a string, hence undo deletion sb.append(top); } else{ //if not really a string, hence undo addition int del=Integer.parseInt(top); int len=sb.length(); String sub=sb.substring(0,len-del); sb.setLength(0); sb.append(sub); } }//type 4 }//for loop }//main
}//Solution
Simple Text Editor
You are viewing a single comment's thread. Return to all comments →
U can check my Solution here:
import java.io.; import java.util.;
public class Solution {
}//Solution