zoukankan      html  css  js  c++  java
  • 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)
    
  • 相关阅读:
    HTB-靶机-Charon
    第一篇Active Directory疑难解答概述(1)
    Outlook Web App 客户端超时设置
    【Troubleshooting Case】Exchange Server 组件状态应用排错?
    【Troubleshooting Case】Unable to delete Exchange database?
    Exchange Server 2007的即将生命周期,您的计划是?
    "the hypervisor is not running" 故障
    Exchange 2016 体系结构
    USB PE
    10 months then free? 10个月,然后自由
  • 原文地址:https://www.cnblogs.com/linshuhui/p/9855297.html
Copyright © 2011-2022 走看看