简体   繁体   English

function 返回第一个可能的 substring 包含 substring 的所有元素

[英]function to return the first possible substring containing all the elements of substring

How can I modify the myStr function to return the first possible substring (traversing from left) containing all the elements of str2 .如何修改myStr function 以返回包含str2的所有元素的第一个可能的 substring (从左侧遍历)。 For example: given str1 = 'xBxxAxxCxxAxCxxBxxxAxCxBxxxAxxBxCx' and str2 = "ABC" , the function should return "BxxAxxC" .例如:给定str1 = 'xBxxAxxCxxAxCxxBxxxAxCxBxxxAxxBxCx'str2 = "ABC" , function 应该return "BxxAxxC"

str1 = input("Enter the first string: ")
str2 = input("Enter the second string to check if the characters exist in the first string: ")

def myStr(str1, str2):
    
    if all(i in str1 for i in str2):
        return True
    else:     
        return False

myStr(str1, str2)

If str2 doesn't contain any duplicates letters, you can map its characters to their first positions in str1 and get the range between the minimum and maximum positions:如果 str2 不包含任何重复字母,您可以将其字符 map 到它们在 str1 中的第一个位置,并获得最小和最大位置之间的范围:

for example:例如:

str1   = 'xBxxAxxCxxAxCxxBxxxAxCxBxxxAxxBxCx'
str2   = "ABC"

pos    = [str1.find(c) for c in str2]
result = str1[min(pos):max(pos)+1]

print(result) # BxxAxxC

If not all characters of str2 are present in str1, this will produce an empty string如果 str1 中没有出现 str2 的所有字符,这将产生一个空字符串

If str2 CAN contain duplicate letters, then the list of positions (pos) needs to include multiple positions for repeated characters:如果 str2 可以包含重复的字母,那么位置列表 (pos) 需要包含多个重复字符的位置:

pos = [i for r in [list(str2)] for i,c in enumerate(str1) 
       if c in r and not r.remove(c)]

Assuming that left-most characters that are not in str2 should be excluded , you could do the following:假设应该在str2中的最左边的字符被排除,您可以执行以下操作:

  • Create a set for the characters in str2str2中的字符创建一个集合
  • Iterate the characters from str1 and remove them from this set.迭代str1中的字符并将它们从该集中删除。
  • Once the set is empty, you know that the condition is fulfilled.一旦集合为空,您就知道条件已满足。
  • To exclude the left most characters, keep track which was the first index where there was a match with a character in the set.要排除最左边的字符,请跟踪与集合中的字符匹配的第一个索引。
def myStr(str1, str2):
    todo = set(str2)
    start = -1
    for i, ch in enumerate(str1):
        if not todo:
            return str1[start:i]
        if ch in todo:
            todo.discard(ch)
            if start < 0:
                start = i

You can use set operations, for each prefix check that all letter are in with您可以使用set操作,为每个前缀检查所有字母是否都在其中

set(str2) - set(str1[:1])

set(str2) - set('xB')       # {'C', 'A'}
set(str2) - set('xBxxA')    # {'C'}
set(str2) - set('xBxxAxxC') # set()
def myStr(str1, str2):
    chars_to_have = set(str2)
    i = 0
    while chars_to_have - set(str1[:i]) != set() and i < len(str1):
        i += 1
    return str1[:i]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM