취미생활/CheckIO

[checkIO]Home 08. Long Repeat

우주먼지의하루 2020. 2. 16. 14:50
728x90

There are four substring missions that were born all in one day and you shouldn’t be needed more than one day to solve them. All of those mission can be simply solved by brute force, but is it always the best way to go? (you might not have access to all of those missions yet, but they are going to be available with more opened islands on the map).

This mission is the first one of the series. Here you should find the length of the longest substring that consists of the same letter. For example, line "aaabbcaaaa" contains four substrings with the same letters "aaa", "bb","c" and "aaaa". The last substring is the longest one which makes it an answer.

Input: String.

Output: Int.

Example:

long_repeat('sdsffffse') == 4

long_repeat('ddvvrwwwrggg') == 3

* 가장 길이가 긴 문자 갯수를 출력하기

* 똑같은 문자로 가장 길게 연결된 것이어야만 한다.

 

 

- 풀이

 

def long_repeat(line):
    mx = 0
    if len(line) == 0: 
        return 0
    i = 0; j = 0
    while i < len(line):
        c = line[i]
        k = 0
        while j < len(line) and line[j] == c:
            j = j + 1
            k = k + 1 
        i = j 
        if k > mx: mx = k
    return mx

 

 

반응형

'취미생활 > CheckIO' 카테고리의 다른 글

[checkIO]Home 11.Bird Language  (0) 2020.02.17
[checkIO]Home 10.Sun Angle  (0) 2020.02.17
[checkIO]Home 07.Flatten a List  (0) 2020.02.16
[checkIO]Home 06.Sort Array by Element Frequency  (0) 2020.02.13
[checkIO]Home 05.Non-unique Elements  (0) 2020.02.11