LeetCode: Decoded String at Index

이원희·2020년 12월 21일
0

📝 PS

목록 보기
29/65
post-thumbnail

문제 풀이

메모리 초과에서 좀 어려웠다.... 이런걸 어떻게 잘 줄일 수 있을까....

class Solution {
    public String decodeAtIndex(String S, int K) {
        long len = 0;

        for (char c : S.toCharArray()) {
            if (Character.isDigit(c)) {
                len *= (c - '0');
            }
            else {
                ++len;
            }
        }

        for (int i = S.length() - 1; i >= 0; i--) {
            K %= len;

            char c = S.charAt(i);

            if (K == 0 && c >= 'a' && c <= 'z') {
                return "" + c;
            }

            if (Character.isDigit(c)) {
                len /= (c - '0');
            }
            else {
                --len;
            }
        }
        return  "";
    }
}

0개의 댓글