티스토리 뷰

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')


'Puzzle > HackerRank' 카테고리의 다른 글

[HackerRank] The Coin Change Problem  (0) 2019.04.19
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함