def backspace_str(word: str) -> str:
new_list = []
for i in word:
if i != '#':
new_list.append(i)
else:
if new_list == []:
return ""
else:
new_list.pop()
return "".join(new_list)
def backspace_str(word: str) -> str:
li = []
for x in word:
if x.isalpha():
li.append(x)
if x == '#' and li:
li.pop()
return "".join(li)
assert backspace_str("a#bc#d") == "bd"
assert backspace_str("abc#d##c") == "ac"
assert backspace_str("abc##d######") == ""
def backspace_str(word: str) -> str:
result = ''
for x in word:
result = result + x if x != '#' else result[:-1]
return result
assert backspace_str("a#bc#d") == "bd"
assert backspace_str("abc#d##c") == "ac"
assert backspace_str("abc##d######") == ""
def backspace_str(word: str) -> str:
record_spec_nums = 0
result = list()
index = 0
if word == '#':
return ''
while index < len(word):
if word[index] != '#':
# 遍历到非#字符而且 # 有记录,需要先做清空# 操作
if record_spec_nums:
if record_spec_nums < len(result):
result = result[:len(result) - record_spec_nums]
else:
result = list()
record_spec_nums = 0
split_info = list()
result.append(word[index])
else:
record_spec_nums += 1
if index == len(word)-1:
if record_spec_nums < len(result):
result = result[:len(result)-record_spec_nums]
else:
result = list()
index += 1
return ''.join(result)