Algorithm/String

[프로그래머스] 문자열 나누기

킹우현 2023. 7. 21. 16:45

def solution(s):
    answer = []
    
    string = s
    
    while(string != ''):
        x = string[0]
        list_str = list(string)
        
        count_x = 0
        count_not_x = 0
        
        index = 0
        
        for i,value in enumerate(list_str):
            if value == x:
                count_x += 1
            else:
                count_not_x += 1
            
            if count_x != 0 and count_x == count_not_x:
                index = i
                break
            
            index = i
            
        answer.append(list_str[:index+1])
        string = ''.join(list_str[index+1:])
    
    return len(answer)

이번 문제는 주어진 조건대로 문자열을 분리하여 총 분리된 문자열의 개수를 구하는 문제이다.

 

주어진 조건대로 구현하여 간단하게 풀이할 수 있던 문제였다 :)