简体   繁体   中英

Python program to Print out the number of times the pattern of a provided Substring matches in a provided String

I have tried to frame a program in Python which would print out the number of times the provided substring appears as a pattern in a provided string .

Unfortunately, my code isn't the best solution as it might not handle some corner cases. I need a solution two-liner or three-liner code maybe) or a modified version to my code.

I also need some suggestions on code optimization if the provided input string is of length 1000 or more.

Input 1 :

" XYZ DF XYZ XY"

"XYZ"

Output 1 :

2

Input 2 :

"AB CDCDC "

" CDC "

Output 2 :

2

My code :

def pattern(string, sub_str ):
    counter = 0
    len_sub = len(sub_str )
    string, sub_str = string.upper(),sub_str.upper()
    for i in range(len(string)):
        sub_loop = ""
        for j in range(i,i+len_sub):
            try:
                sub_loop+=string[j] 
                if sub_loop == sub_str:
                    counter+=1
            except:
                pass
    return counter

if __name__ == '__main__':
    string = input().strip()
    sub_str = input().strip()
    count= pattern(string, sub_str )
    print(count)

For overlapping patterns:

For overlapping cases we will use regex's Positive Lookahead (?=) :

sub_str = "CDC"
string = "ABCDCDC"
len(re.findall(f"(?=({sub_str}))", string))

more info can be found here: https://www.regular-expressions.info/lookaround.html

Without overlapping:

Python's str has a builtin function count that let you count the number of substring occurrences in the original string.

From the function documentation:

S.count(sub[, start[, end]]) -> int

Return the number of non-overlapping occurrences of substring sub in
string S[start:end].  Optional arguments start and end are
interpreted as in slice notation.

So eventually, all you have to do is:

"XYZDFXYZXY".count("XYZ")

so in total:

if __name__ == '__main__':
    string = input().strip()
    sub_str = input().strip()
    count = string.count(sub_str)
    print(count)

An easy solution is to use the split() and then get the length of the resulting array minus 1. In your code above:


def pattern(string, sub_str ):

    string, sub_str = string.upper(),sub_str.upper()
    Outarray= string.split(sub_str)
    counter = len(outarry) - 1
    Return counter

Using List Comprehension reduced the whole code to one line.

def pattern(string, sub_str):
    counter = sum([1 for i in range(len(string)) if string[i:].startswith(sub_str)])
    return counter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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