Untitled
class Solution { public String stringShift(String s, int[][] shift) { int sum = 0; for (int[] sh : shift) { if (sh[0] == 1) { sum += sh[1]; } else { sum -= sh[1]; } } int n = s.length(); char[] answer = new char[n]; if (sum < 0) { int j = n - 1; for (int i = 0; i < -sum; i++) { answer[j--] = s.charAt(i); } j = 0; for (int i = -sum; i < n; i++) { answer[j++] = s.charAt(i); } } else { int j = 0; for (int i = n - 1; i >= n - sum; i--) { answer[j++] = s.charAt(i); } for (int i = 0; j < n; i++, j++) { answer[j] = s.charAt(i); } } return new String(answer); } }
Leave a Comment