New KeyBoard (Hack The Interview V : Asia-Pacific)
Contest Link
import java.util.*; class Solution{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String s = sc.next(); StringBuilder res = new StringBuilder(""); boolean isPressed = false; int count = 0; boolean cursorStart = false; ArrayList<Character> c = new ArrayList<>(); for(int i=0;i<10;i++){ c.add((char)i); } for(int i=0;i<s.length();i++){ if(s.charAt(i)=='<'){ res = res.reverse(); cursorStart = true; continue; } else if(s.charAt(i)=='>'){ if(cursorStart == true){ res = res.reverse(); cursorStart = false; } continue; } else if(s.charAt(i)=='*'){ res.delete(res.length()-1, res.length()); } else if(s.charAt(i)=='#'){ count++; if(count%2!=0){ isPressed = true; } else{ isPressed = false; } } else{ if(c.contains(s.charAt(i))){ if(isPressed){ continue; } else{ res.append(s.charAt(i)); } } res.append(s.charAt(i)); } } System.out.println(res); } }
Comments
Post a Comment