Algorithm

파일명 정렬

yerinpark 2023. 8. 3. 18:26

파싱까지 함

import java.util.Arrays;

class Solution {
    public static String[] solution(String[] files) {
        String[] answer = {};


        // files 파싱
        int len = files.length;

        String[] head = new String[files.length];
        String[] num = new String[files.length];
        String[] tail = new String[files.length];

        for(int i = 0; i < len; i++) {
            files[i] = files[i].toUpperCase();

            int cnt = 0;

            // head 담기
            for(int j = 0; j < files[i].length(); j++) {
                if(!Character.isDigit(files[i].charAt(j))) {
                    cnt++;
                } else {
                    break;
                }
            }

            head[i] = files[i].substring(0, cnt);
            files[i] = files[i].substring(cnt);

            cnt = 0;

            // number 담기
            for(int j = 0; j < files[i].length(); j++) {
                if(Character.isDigit(files[i].charAt(j))) {
                    cnt++;
                } else {
                    break;
                }
            }
            num[i] = files[i].substring(0, cnt);
            files[i] = files[i].substring(cnt);


            // tail 담기
            tail[i] = files[i];
        }
        return answer;
    }

    public static void main(String[] args) {
        String[] files = {"img12.png", "img10.png", "img02.png", "img1.png", "IMG01.GIF", "img2.JPG"};
//        String[] files = {"F-5 Freedom Fighter", "B-50 Superfortress", "A-10 Thunderbolt II", "F-14 Tomcat"};

        solution(files);
    }
}

'Algorithm' 카테고리의 다른 글

[Programmers] 타겟 넘버 Java  (0) 2023.10.09
DFS와 BFS  (0) 2023.10.09
[프로그래머스] 방금 그 곡 Java  (0) 2023.08.01
[프로그래머스] 캐시  (0) 2023.07.18
[프로그래머스] Java 요격 시스템  (0) 2023.06.30