Puzzle/HackerRank

[HackerRank] Super Reduced String

More Code 2019. 4. 19. 04:25

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



# Python 3


def reduce_string(s):
for i, x in enumerate(s[1:]):
if s[i] == x:
return reduce_string(s[:i] + s[i + 2:])
return s


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



# Python 3


def reduce_string(s):
str_list = list(s)
index = 0
while len(str_list) > 1 and index < len(str_list) - 1:
if str_list[index] == str_list[index + 1]:
str_list.pop(index + 1)
str_list.pop(index)
index = index - 1 if index > 0 else 0
else:
index += 1
return ''.join(str_list)


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



# Python 3


def reduce_string(s):
str_list = []
for x in s:
if str_list and str_list[-1] == x:
str_list.pop()
else:
str_list.append(x)
return ''.join(str_list)


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