684. 缺少的字符串

684. 缺少的字符串

中文 English

给出两个字符串,你需要找到缺少的字符串

样例

样例 1:

输入 : str1 = "This is an example", str2 = "is example"
输出 : ["This", "an"]

注意事项

输出时字符串顺序为输入字符串内的顺序

class Solution:
    """
    @param str1: a given string
    @param str2: another given string
    @return: An array of missing string
    """
    '''
    大致思路:
    1.因为是判断缺少的字符串,所以需要是循环到空格的时候为一个完整的字符串
    2.如果是循环到一个完整的字符串的时候,append完之后要置空,不管在里面或者不在里面(注意:判断是否在里面必须是一个一个切割好的字符串,否则如果是a的话判读是否在一个字符串an里面也是成立的)
    '''
    def missingString(self,str1,str2):
        ##首先需要将str2切割好
        s2 = []
        n=''
        for j in str2+' ':
            if j == ' ':
                s2.append(n)
                n=''
            else:
                n+=j
        
        s = str1+' '
        column = ''
        res = []
        for i in s:
            ##说明是到了一个完整的字符串
            if i == ' ':
                if column not in s2:
                    res.append(column)
                #假如在里面或者不在里面,都需要进行置空
                column =''
            else:
                column = column+i
        return res

猜你喜欢

转载自www.cnblogs.com/yunxintryyoubest/p/12502978.html