Problem Solving/Programmers

[Level 2] 조이스틱

kmkunk 2022. 3. 31. 16:37
class Solution {
    public int solution(String name) {
        int answer = 0;
        int count = name.length()-1;
        
        for(int i=0; i<name.length(); i++) {
            char c = name.charAt(i);
            int index = i+1;
            
            answer += c<'N' ? (c-'0')-('A'-'0') : 26-(c-'0')+('A'-'0');
            
            while(index<name.length() && name.charAt(index)=='A') { index++; }
            
            count = Math.min(count, name.length()-index+i*2);
            count = Math.min(count, (name.length()-index)*2+i);
        }
        
        return answer+count;
    }
}