【每日一题1014】退格字符串

:mage:t2:假设字符串中的”#“符号表示的是一个退格(删除),例如在字符串"a#bc#d"其实最终结果是"bd"。请编写一个函数,接收一个含有退格符号的字符串,输出退格后的最终结果。

示例:
输入:" abc#d##c",输出:“ac”
输入:“abc##d######”,输出:“”
题目难度:简单
题目来源:codewars : Backspaces in string

def backspace_str(word: str) -> str:
    pass

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_list = []
    for s in word:
        if s != '#':
            result_list.append(s)
        else:
            if len(result_list) > 0:
                result_list.pop()
    return ''.join(result_list)


assert backspace_str("a#bc#d") == "bd"
assert backspace_str("abc#d##c") == "ac"
assert backspace_str("abc##d######") == ""
1 个赞
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:
    # 获取整个字符串的长度
    len_word = len(word)
    # 通过删除所有 # 字符
    # 获取字符串中所有字母的长度
    len_str = len(word.replace("#",""))
    # 【特殊情况】如果字母的长度未超过整个字符串长度的一半,则直接返回空字符
    if len_str <= len_word/2:
        return ""
    # 定义储存字母索引的列表
    list_index = []
    # 将整个字符串转为列表
    list_word = list(word)
    # 遍历字符串列表
    for i in range(len_word):
        if list_word[i] == "#":
            # 该字符为 # 则将自身置为空字符
            list_word[i] = ""
            # 取左边离 # 字符最近的一位字母索引
            j = list_index[-1]
            # 将该字母置为空字符
            list_word[j] = ""
            # 然后已被删除的字母索引也从索引列表中删除
            list_index.pop()
        else:
            # 将字母的索引放入索引储存列表中
            list_index.append(i)
    # 将处理后的列表转回为字符串
    return "".join(list_word)


assert backspace_str("a#bc#d") == "bd"
assert backspace_str("abc#d##c") == "ac"
assert backspace_str("abc##d######") == ""
def backspace_str(word):
    list_word = list(word)
    i = 0
    while i != len(list_word):
        if list_word[i] == "#":
            list_word.pop(i)
            list_word.pop(i-1)
            i = 0
            continue
        i = i + 1
    return "".join(list_word)



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:
    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:
    list=[]
    for i in word:
        if i!="#":
            list.append(i)
        else:
            if list!=[]:
                list.pop(-1)

    return ''.join(list)



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 i in word:
        if i != '#':
            result.append(i)
        else:
            if len(result) > 0:
                result.pop()
    return ''.join(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)