Facebook
From Askar, 4 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 145
  1. class Solution {
  2.     public String stringShift(String s, int[][] shift) {
  3.         Deque<Character> chars = new ArrayDeque<>();
  4.         for (char c : s.toCharArray()) {
  5.             chars.push(c);
  6.         }
  7.  
  8.         for (int[] ar : shift) {
  9.             for (int i = 0; i < ar[1]; i++) {
  10.                 if (ar[0] == 1) {
  11.                     Character c = chars.pollFirst();
  12.                     chars.addLast(c);
  13.                 } else {
  14.                     Character c = chars.pollLast();
  15.                     chars.addFirst(c);
  16.  
  17.                 }
  18.             }
  19.         }
  20.         StringBuilder sb = new StringBuilder();
  21.         Character last = chars.pollLast();
  22.         while (last != null) {
  23.             sb.append(last);
  24.             last = chars.pollLast();
  25.         }
  26.         return sb.toString();
  27.     }
  28. }