字符串Ⅱ
Posted oldby
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串Ⅱ相关的知识,希望对你有一定的参考价值。
四、子序列
? 1.最长子序列
? 给定一个字符串‘s’和一个整数k,找到其他字符串‘t’,使得‘t’是给定字
符串‘s’的最大子序列,同时‘t’的每个字符在字符串s中必须至少出现k次。
? Input: s = "baaabaacba“, k = 3
? Output : baaabaaba
import collections def longestSub(s, k): result = list() c = collections.Counter(s) for i in s: if (c[i] >= k): result.append(i) return "".join(result)
2.检查子序列
? 给定两个字符串str1和str2。 确定str1是否是str2的子序列。 子序列是可以通
过删除一些元素而不改变其余元素的顺序从另一个序列派生的序列。
? Input: str1 = "AXY", str2 = "ADXCPY"
? Output: True (str1 是 str2的子序列)
? Input: str1 = "AXY", str2 = "YADXCP"
? Output: False (str1 不是str2的子序列)
方法一:递归
def isSubSequence(string1, string2, m, n): # Base Cases if m == 0: return True if n == 0: return False # If last characters of two strings are matching if string1[m-1] == string2[n-1]: return isSubSequence(string1, string2, m-1, n-1) # If last characters are not matching return isSubSequence(string1, string2, m, n-1)
方法二:双指针
def isSubSequence(str1, str2): m = len(str1) n = len(str2) j = 0 # Index of str1 i = 0 # Index of str2 while j < m and i < n: if str1[j] == str2[i]: j = j + 1 i = i + 1 return j == m
? 给一个字典和一个字符串‘str’,找到字典中最长的字符串,它可以通过删除给
定的‘str’中的一些字符来形成。
? Input: dict = {"ale", "apple", "monkey", "plea"} ,
? str = "abpcplea"
? Output : apple
def findLongestString(words, s): result = "" length = 0 for w in words: if length < len(w) and isSubSequence(w, s): result = w length = len(w) return result
? 给定一列n个整数.。找出所有子列元素之和的加和。
‘‘‘
Input : arr[] = { 6, 8, 5 }
Output : 76
All subsequence sum are:
{ 6 }, sum = 6
{ 8 }, sum = 7
{ 5 }, sum = 6
{ 6, 8 }, sum = 14
{ 6, 5 }, sum = 11
{ 8, 5 }, sum = 13
{ 6, 8, 5 }, sum = 19
Total sum = 76.
‘‘‘
Input : arr[] = { 6, 8, 5 }
Output : 76
All subsequence sum are:
{ 6 }, sum = 6
{ 8 }, sum = 7
{ 5 }, sum = 6
{ 6, 8 }, sum = 14
{ 6, 5 }, sum = 11
{ 8, 5 }, sum = 13
{ 6, 8, 5 }, sum = 19
Total sum = 76.
‘‘‘
def sumSub(arr): ans = sum(arr) return ans * pow(2, len(arr) - 1)
五、模式搜索
1. strStr
方法一:
def strStr(text, pattern): for i in range(len(text) - len(pattern)+1): if text[i:i+len(pattern)] == pattern: return i return -1
另:
def strStr(text, pattern): """ Brute force algorithm. Time complexity: O(n * m). Space complexity: O(1), where m, n are the lengths of text and pattern. """ n, m = len(text), len(pattern) for i in range(n - m + 1): start = i for j in range(m): if text[i] != pattern[j]: break i += 1 else: # no break occured, i.e. match was found return start return -1
方法二:滚动哈希
# Rolling Hash def strStr(text, pattern): base = 29 patternHash = 0 tempBase = 1 hayHash = 0 for i in range(len(pattern) - 1, -1, -1): patternHash += ord(pattern[i]) * tempBase tempBase *= base tempBase = 1 for i in range(len(pattern) - 1, -1, -1): hayHash += ord(text[i]) * tempBase tempBase *= base if patternHash == hayHash and text[0:len(pattern)] == pattern: return 0 tempBase /= base for i in range(len(pattern), len(text)): hayHash = (hayHash - ord(text[i - len(pattern)]) * tempBase) * base + ord(text[i]) if hayHash == patternHash and text[i-len(pattern)+1:i+1] == pattern: return i - len(pattern) + 1 return -1
2. 敏感词
? 给定的句子作为输入,用星号‘*’检查替换的单词。
def censor(text, word): word_list = text.split() result = ‘‘ stars = ‘*‘ * len(word) count = 0 index = 0; for i in word_list: if i == word: word_list[index] = stars index += 1 # join the words result =‘ ‘.join(word_list) return result
3.用C替换所有出现的字符串AB
? 给定一个可能包含一个“AB”的字符串str。 将str中的所有“AB”替换为“C”。
def translate(st) : l = len(st) if (l < 2) : return i = 0 # Index in modified string j = 0 # Index in original string while (j < l - 1) : # Replace occurrence of "AB" with "C" if (st[j] == ‘A‘ and st[j + 1] == ‘B‘) : # Increment j by 2 j += 2 st[i] = ‘C‘ i += 1 continue st[i] = st[j] i += 1 j += 1 if (j == l - 1) : st[i] = st[j] i += 1 # add a null character to # terminate string return i
4. 数出“1(0+)1”模式的发生次数
给定一个字母数字字符串,找出给定字符串中出现模式1(0+)1的次数。 这里,
(0+)表示存在连续0的非空序列。
def patternCount(s): last = s[0] i = 1 counter = 0 while (i < len(s)): # We found 0 and last character was ‘1‘, # state change if (s[i] == ‘0‘ and last == ‘1‘): while (s[i] == ‘0‘ and i < len(s)): i += 1 if (i == len(s)): return counter # After the stream of 0‘s, we got a ‘1‘, # counter incremented if (s[i] == ‘1‘): counter += 1 # Last character stored last = s[i] i += 1 return counter
5.与通配符匹配的字符串
? 给定两个字符串,其中第一个字符串可以包含通配符字符,第二个字符串是普通
字符串 编写一个函数,如果两个字符串匹配,则返回true。 第一个字符串中允
许使用以下通配符。
? * --> Matches with 0 or more instances of any character or set of
characters.
? ? --> Matches with any one character.
def match(first, second): if len(first) == 0 and len(second) == 0: return True # Make sure that the characters after ‘*‘ are present # in second string. This function assumes that the first # string will not contain two consecutive ‘*‘ if len(first) > 1 and first[0] == ‘*‘ and len(second) == 0: return False # If the first string contains ‘?‘, or current characters # of both strings match if (len(first) > 1 and first[0] == ‘?‘) or (len(first) != 0 and len(second) !=0 and first[0] == second[0]): return match(first[1:],second[1:]); # If there is *, then there are two possibilities # a) We consider current character of second string # b) We ignore current character of second string. if len(first) !=0 and first[0] == ‘*‘: return match(first[1:],second) or match(first,second[1:])