요즘 틈틈이 알고리즘 기초 공부를 하고 있는데, HackerRank 문제를 풀면서 나의 풀이를 기록으로 남겨보려 한다.


Problem :

https://www.hackerrank.com/challenges/reduced-string/problem


My Solution :

#!/usr/bin/env python3

def super_reduced_string(s, i):
    if i < 0:
        i = 0
    while i < len(s) - 1:
        if s[i] == s[i+1]:
            return super_reduced_string(s[:i] + s[i+2:], i-1)
        i += 1
    return s


s = input().strip()
result = super_reduced_string(s, 0)
print(result) if result != '' else print('Empty String')


My Solution2 :

#!/usr/bin/env python3

def super_reduced_string(s):
    res = []
    for c in s:
        if res and c == res[-1]:
            res.pop()
        else:
            res.append(c)
    return ''.join(res)


s = input().strip()
result = super_reduced_string(s)
print(result) if result != '' else print('Empty String')