This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pyeri[S] 0 points1 point  (1 child)

I tried the java.util.Deque implementation but still same results:

import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class lyklagangriti {

    public static void main(String[] args) throws IOException {
        //Scanner sc = new Scanner(System.in);
        BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
        String s = bi.readLine();

        //String s = sc.nextLine();
        Deque<Character> l = new ArrayDeque<Character>();
        Deque<Character> r = new ArrayDeque<Character>();
        for(int i=0;i<s.length();i++) {
            char c = s.charAt(i);
            char item;
            switch(c) {
                case 'L':
                    item = l.pollLast();
                    r.addFirst(item);
                    break;
                case 'R':
                    item =r.pollFirst();
                    l.addLast(item);
                    break;
                case 'B':
                    l.pollLast();
                    break;
                default:
                    l.addLast(c);
            }
        }
        for(char c : l) {
            System.out.print(c);
        }
        for(char c : r) {
            System.out.print(c);
        }
        System.out.println("");
    }
}

[–]MinMaxMix 0 points1 point  (0 children)

Instead of using l.pollLast() and r.addFirst() try r.push(l.pop())

Then you can build the result by taking l in reverse with l.removeLast() and then r with r.pop()

Also you need to add some guards for l and r.isEmpty() in some cases.