기록하는 공간

[leetcode/java] 1427.Perform String Shifts 본문

알고리즘/leetcode

[leetcode/java] 1427.Perform String Shifts

llollhh_ 2020. 4. 15. 15:25

substring 함수를 이용한 풀이

class Solution {
    
    private String takeOrtakeLast(int direction, int amount, String s) {
        int strLength = s.length();
        
        if (direction == 0) {
            s = s.substring(amount, strLength) + s.substring(0 , amount);
        } else {
            s = (s.substring(strLength - amount, strLength) + s).substring(0, strLength);
        }
        return s;
    }

    public String stringShift(String s, int[][] shift) {
        for (int i = 0; i < shift.length; i++) {
            s = takeOrtakeLast(shift[i][0], shift[i][1], s);          
        }
        
        return s;
    }
    
}
Comments