๊ฐœ๋ฐœ ์ฅฌ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/Java] 77886. 110 ์˜ฎ๊ธฐ๊ธฐ ๋ณธ๋ฌธ

์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/Java] 77886. 110 ์˜ฎ๊ธฐ๊ธฐ

DevJuice 2025. 1. 24. 18:09
๋ฐ˜์‘ํ˜•

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ: https://school.programmers.co.kr/learn/courses/30/lessons/77886

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

 

 

๐Ÿ”  ํ•ด๊ฒฐ ๊ณผ์ •

๋ฐฐ์—ด s์˜ ๊ธธ์ด์˜ ์ตœ๋Œ“๊ฐ’์ด 100๋งŒ์ด๊ณ , s์˜ ๋ชจ๋“  ์›์†Œ์˜ ๊ธธ์ด์˜ ํ•ฉ์ด 100๋งŒ์ด๋ผ๋Š” ์–˜๊ธฐ์— ์ฃผ์˜์ ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

 

์œ„ ์–˜๊ธฐ์˜ ์˜๋ฏธ๋Š” s์˜ ๋ฐฐ์—ด์ด ์ตœ๋Œ€ 100๋งŒ์ด๋ผ๋ฉด s์˜ ์›์†Œ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 1์ด ๋œ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ ์ด ๋ฌธ์ œ๋Š” ๋‹จ์ˆœ ๊ตฌํ˜„์œผ๋กœ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๊ตฌํ˜„ ๊ณผ์ •์˜ ํ•ต์‹ฌ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1๏ธโƒฃ s์˜ ์›์†Œ ํ•˜๋‚˜๋ฅผ ๊บผ๋‚ด "110"์˜ ํŒจํ„ด์„ ๊ฐ€์ง„ ๋ฌธ์ž์—ด์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„์„œ 110์„ ๊ณ„์† ์ œ๊ฑฐํ•œ๋‹ค.
2๏ธโƒฃ ์ œ๊ฑฐํ•œ 110์˜ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค.
3๏ธโƒฃ 110์„ ์ œ๊ฑฐํ•œ ์ตœ์ข… ์›์†Œ a๋Š” 110์˜ ํŒจํ„ด์„ ๊ฐ€์ง€์ง€ ์•Š์„ ๊ฒƒ์ด๋ฏ€๋กœ 110์„ ๋„ฃ์„ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค.
4๏ธโƒฃ ์‚ฌ์ „์ˆœ์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์–ด์•ผํ•˜๋ฏ€๋กœ a์—์„œ 0์ด ๋งˆ์ง€๋ง‰์— ๋“ฑ์žฅํ•œ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค.
5๏ธโƒฃ ๋งˆ์ง€๋ง‰ 0์œ„์น˜ ๋’ค์— 110์„ ๊ณ„์† ์ด์–ด๋ถ™์—ฌ์ค€๋‹ค.
6๏ธโƒฃ ๋งŒ์•ฝ a์—์„œ 0์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด "110"ํŒจํ„ด์—์„œ 0์ด ์กด์žฌํ•˜๋ฏ€๋กœ ์‚ฌ์ „์ˆœ์— ๋”ฐ๋ผ์„œ a์•ž์— "110"์„ ๋ถ™์—ฌ์ค€๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด์„œ s = "0111111010"์˜ ๋ฌธ์ž์—ด์ด ์žˆ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์œผ๋กœ ๋‹ต์„ ๋„์ถœํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

1๏ธโƒฃ "110" ํŒจํ„ด ์ œ๊ฑฐ ํ›„ s ์ƒํƒœ: "0111110",    ์ œ๊ฑฐํ•œ "110" ํšŸ์ˆ˜: 1
2๏ธโƒฃ "110" ํŒจํ„ด ์ œ๊ฑฐ ํ›„ s ์ƒํƒœ: "0111",          ์ œ๊ฑฐํ•œ "110" ํšŸ์ˆ˜: 2
3๏ธโƒฃ "110" ํŒจํ„ด์ด ์•ˆ๋ณด์ด๋ฏ€๋กœ "0111"์—์„œ 0์˜ ๋งˆ์ง€๋ง‰ ์œ„์น˜์ธ 0๋ฒˆ ์ธ๋ฑ์Šค ๋’ค์— "110" ํŒจํ„ด์„ ๋„ฃ์–ด๋‘”๋‹ค.
4๏ธโƒฃ "110" ์‚ฝ์ž… ํ›„ s ์ƒํƒœ: "0110111",            ๋‚จ์€ "110" ํšŸ์ˆ˜: 1,     ๋งˆ์ง€๋ง‰ '0'์˜ ์ธ๋ฑ์Šค: 3
5๏ธโƒฃ  ๋งˆ์ง€๋ง‰ '0' ์ธ๋ฑ์Šค ์œ„์น˜ ๋’ค์— "110" ์‚ฝ์ž… ํ›„ s ์ƒํƒœ: "0110110111",       ๋‚จ์€ "110" ํšŸ์ˆ˜: 0

 

๊ฒฐ๊ณผ์ ์œผ๋กœ "0110110111"์˜ ๋ฌธ์ž์—ด์ด ์‚ฌ์ „์ˆœ์œผ๋กœ ์ œ์ผ ์•ž์„  ๋ฌธ์ž์—ด์„ ์œ ์ง€ํ•˜๋ฉด์„œ "110"์˜ ์œ„์น˜๋ฅผ ์˜ฎ๊ธด ๊ฒƒ์ด ๋ฉ๋‹ˆ๋‹ค.

 

"110"์„ ๊ณ„์† ์ œ๊ฑฐํ•œ ๋ฌธ์ž์—ด ๊ฒฐ๊ณผ ์ •๋ณด์™€ ์ œ๊ฑฐํ•œ "110" ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ class๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ•ด๋‹น ์ •๋ณด๋“ค์„ ์ €์žฅํ•œ ๋‹ค์Œ "110"์„ ์œ„ ๊ณผ์ •์— ๋”ฐ๋ผ ๋ถ™์—ฌ์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“’ ํšŒ๊ณ ์ 

"110"์˜ ์œ„์น˜๋ฅผ ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ์‚ฌ์ „์ˆœ์œผ๋กœ ์œ ์ง€ํ•˜๋ฉด์„œ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ์„์ง€ ๊ณ ๋ฏผ์ด ์ข€ ๊ธธ์—ˆ๋˜ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ๊ทผ๋ณธ์ ์œผ๋กœ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ ์šฐ๋ ค ๋•Œ๋ฌธ์— ๊ณ ๋ฏผ์ด ์žˆ์—ˆ์ง€๋งŒ ์•ž์œผ๋กœ ์กฐ๊ฑด์„ ์ข€ ๋” ์ƒ์„ธํ•˜๊ฒŒ ๋ด์•ผ๊ฒ ๋‹ค๋Š” ๋ฐ˜์„ฑ์„ ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  s์˜ ์›์†Œ์—์„œ "110" ํŒจํ„ด ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•œ ํ›„ ๊ทธ ์ดํ›„์˜ ํŒจํ„ด์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ์ธ๋ฑ์‹ฑ ๊ณผ์ •์—์„œ ์—๋Ÿฌ ์‚ฌํ•ญ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์‹ฑ ๊ด€๋ จ ๋ณ€์ˆ˜๊ฐ€ ์Œ์ˆ˜๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ๋Š”๋ฐ ๋ณ€ํ˜•๋œ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ์ธ๋ฑ์‹ฑ ๋ฒ”์œ„ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ™•์‹คํ•˜๊ฒŒ ํ•ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์„ ํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 


โœ๏ธ ์ฝ”๋“œ

import java.util.*;

/**
110์„ ๋ฝ‘์•„์„œ ์ž„์˜์˜ ์œ„์น˜์— ๋‹ค์‹œ ์‚ฝ์ž…ํ•จ.
์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฐ€์žฅ ์•ž์„œ๋Š” ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค๊ธฐ
**/
class Remove110 {
    private final StringBuilder afterRemove;
    private final int count110;
    
    public Remove110(StringBuilder afterRemove, int count110) {
        this.afterRemove = afterRemove;
        this.count110 = count110;
    }
    
    public StringBuilder getAfterRemove() {
        return afterRemove;
    }
    
    public int getCount110() {
        return count110;
    }
}

class Solution {
    
    public List<String> solution(String[] s) {
        List<String> answer = new ArrayList<>();
        
        // s์˜ ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด๋ณด๊ธฐ: ๋ฐ˜๋ณต๋ฌธ (๋ฐฐ์—ด์˜ ๊ธธ์ด: 100๋งŒ)
        for (String input : s) {
            Remove110 afterInfo = remove110(input); // 110์„ ์ œ๊ฑฐํ•œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด
            
            if (afterInfo == null) {
                continue;
            }
            
            int count110 = afterInfo.getCount110();
            StringBuilder afterRemove = afterInfo.getAfterRemove();
            int afterLen = afterRemove.length();
            int zeroIdx = -1;
            
            for (int i = afterLen - 1; i >= 0; --i) {
                if (afterRemove.charAt(i) == '0') {
                    zeroIdx = i;
                    break;
                }
            }
            
            // ํ•ด๋‹น ์ž๋ฆฌ ๋’ค์— count ๋งŒํผ ๊ฒฐ๊ณผ๋ฅผ ๋„ฃ์–ด๋‘๊ธฐ
            for (int i = 0; i < count110; ++i) {
                afterRemove.insert(zeroIdx + 1, "110");
                zeroIdx += 3;
            }
            
            answer.add(afterRemove.toString());
        }
        
        return answer;
    }
    
    private Remove110 remove110(final String input) {
        StringBuilder sb = new StringBuilder(input);
        int count110 = 0;
        
        int i = 0;
        while (i <= sb.length() - 3) {
            if (sb.charAt(i) == '1' && sb.charAt(i + 1) == '1' && sb.charAt(i + 2) =='0') {
                count110++;
                sb.delete(i, i + 3);
                // ๋‹ค์‹œ ์กฐ๊ธˆ ์•ž์œผ๋กœ ๋Œ์•„๊ฐ€์„œ ์žฌ๊ฒ€์‚ฌ ํ•„์š”:
                i = Math.max(0, i - 2);  // or i--, i-2 ๋“ฑ
            } else {
                i++;
            }
        }

        return new Remove110(sb, count110);
    }
}
๋ฐ˜์‘ํ˜•