2023-04-21 이상한 문자 만들기

Summary

StringBuilder 를 이용하여 python 의 list.append() 와 같이 편리한 로직으로 해결이 가능함.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
    public String solution(String s) {
        char[] arr = s.toCharArray();
        StringBuilder builder = new StringBuilder();
        
        int idx = 0;
        for (int i=0; i<arr.length; i++) {
            if (arr[i] == ' ') {
                idx = 0;
                builder.append(' ');
                continue;
            }
            if (idx % 2 == 0) builder.append(Character.toUpperCase(arr[i]));
            else builder.append(Character.toLowerCase(arr[i]));
            idx += 1;
        }
        
        return builder.toString();
        
    }
}

comments powered by Disqus