python-algorithm

백준 7120 String

무적김두칠 2022. 12. 5. 22:00

https://www.acmicpc.net/problem/7120

 

7120번: String

It sometimes happens that a button on the computer keyboard sticks and then in the printed text there are more than one identical letters. For example, the word "piano" can change into "ppppppiaanooooo". Your task is to write a program that corrects these

www.acmicpc.net

 

 
1
2
3
4
5
6
7
8
9
10
def sol(s):
    answer = ''
    for i in range(1,len(s)):
        if s[i-1]!=s[i]:
            answer +=s[i-1]
    return answer+s[-1]
if __name__ == '__main__':
    s = input()
    print(sol(s))
 
cs

반복문을 통해서 동일하지 않은 문자열을 찾고, 가장 마지막 문자는 반복문을 통해 해결이 안돼니 따로 추가 

Search for non-dentical strings through a loop statement, and add the last character separately because it cannot be resolved through a loop statement.

반응형