• lintCode字符串处理


    一、两个字符串是变位词

    写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串。
    
    样例
    给出 s = "abcd",t="dcab",返回 true.
    给出 s = "ab", t = "ab", 返回 true.
    给出 s = "ab", t = "ac", 返回 false.
    

    坑:sorted函数排序字符串返回的是列表

    def fun(s, t):
        return hash(''.join(sorted(s))) == hash(''.join(sorted(t)))
    

    二、乱序字符串

    题目内容:

    给出一个字符串数组S,找到其中所有的乱序字符串(Anagram)。如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中。
    
    样例
    对于字符串数组 ["lint","intl","inlt","code"]
    
    返回 ["lint","inlt","intl"]
    

    求解

    小坑:刚开始看题意以为里面只有一组乱序字符串,实际上可能有多组。
    求解思路:

    1、先创建一个空的默认字典,默认值是空列表
    2、遍历数组,将每个数值排序后,作为键,本身的值作为值。
    3、循环字典,当列表长度大于1,增加到返回列表
    坑:字符串用排序函数后返回的是列表!
    相关知识:默认字典的使用
    

    代码如下:

    def get_list(l):
        from collections import defaultdict
        d = defaultdict(list)
    
        for i in l:
            k = ''.join(sorted(i))
            d[k].append(i)
        return [j for k in d for j in d[k] if len(d[k]) > 1]
    

    三、比较字符串

    比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母
    
    样例
    给出 A = "ABCD" B = "ACD",返回 true
    
    给出 A = "ABCD" B = "AABC", 返回 false
    
    def fun(a, B):
        from collections import Counter
        counter_a = Counter(A)
        counter_b = Counter(B)
        for k in counter_b:
            if counter_a[k] > counter_b.get(k, 0):
                return False
        return True
    

    四字符串查找

    对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1。
    
    样例
    如果 source = "source" 和 target = "target",返回 -1。
    
    如果 source = "abcdabcdefg" 和 target = "bcd",返回 1。
    

    暴力解法:

    def strStr(self, source, target):
            # Write your code here
            if not target:return 0
            for i in range(len(source)):
                x = i
                for j in range(len(target)):
                    if source[x] == target[j]:
                        x += 1
                        j += 1
                        if j == len(target): return i
                        if x == len(source):break  #注意当匹配到原字符串结尾时要退出,否则索引超出报错
                    else:break
                    
                else:
                    return i
            return -1
    
    

    进阶解法

    使用KMP算法,也就是i指针不回溯,下面的j指针根据一个next数组来回溯。 
    

    五、 最长公共子串

    给出两个字符串,找到最长公共子串,并返回其长度。
    样例
    给出A=“ABCD”,B=“CBCE”,返回 2
    注意事项
    子串的字符应该连续的出现在原字符串中,这与子序列有所不同。
    
    #思路:求出两个字符串的所有子串,然后用求交集,然后求出集合中最长的返回即可
    def longest_son_seq(s1, s2):
        l1 = [s1[i:j + 1] for i in range(len(s1)) for j in range(i, len(s1))]
        l2 = [s2[i:j + 1] for i in range(len(s2)) for j in range(i, len(s2))]
        print(len(max((set(l1) & set(l2) or ['']), key= len)))
    

    六最长公共前缀

    #思路:利用上题思路,求每个字符串的前缀子串,然后求交集。 
    #坑:交集为空,输入字符串列表为空
    def longComentPrefix(strs):
        """
        :param strs: a list of string
        :return: the longest common prefix
        """
        from functools import reduce
        if not strs:return ''
        str_list = [[s[:j + 1] for j in range(len(s))] for s in strs]
        com_set = reduce(lambda x, y: set(x) & set(y), str_list)
        return max(com_set or [''], key=len)
    
  • 相关阅读:
    linux docker常用命令
    angular input输入框自动消除前后空格
    angular 一览数据全选符合条件数据
    linux MySQL出现错误的一些常见解决办法
    linux crontab定时任务一些命令
    Linux下定时访问指定url地址
    直接打印对象的结果
    java中变量的作用域
    请求转发后地址栏显示传入的值,页面不显示的原因
    String不是基本数据类型,但是
  • 原文地址:https://www.cnblogs.com/linshuhui/p/9855297.html
Copyright © 2020-2023  润新知