标签:continue original span any pow count case 指针 ssi
四、子序列
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.检查子序列
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
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
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
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”模式的发生次数
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.与通配符匹配的字符串
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:])
标签:continue original span any pow count case 指针 ssi
原文地址:https://www.cnblogs.com/oldby/p/12991599.html