0

Given a pair of chars,we have to find the number of the pair in a string

Example:

string:xyxyxxxyxyxy

pair:xy

we also have to find if there is a reverse of that pair

xy->5 ans

here's the code I tried(actually the pair is fixed->xy or yx)

    try:
        for _ in range(int(input())):
            s = input()
            l=[]
            if len(s)%2==0:
                l=[(s[i:i+2]) for i in range(0, len(s), 2)]
            else:
                l=[(s[1:][i:i+2]) for i in range(0, len(s), 2)]
            if s[:-2]=='xy' or s[:-2]=='yx':
                print(l.count('xy')+l.count('yx')+1)
            else:
                print(l.count('xy') + l.count('yx'))
    except:
        pass

Any help would be appreciated

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • What is your output? – po.pe Jun 08 '20 at 09:25
  • Does this answer your question? [Count number of occurrences of a given substring in a string](https://stackoverflow.com/questions/8899905/count-number-of-occurrences-of-a-given-substring-in-a-string) – Craicerjack Jun 08 '20 at 09:30

0 Answers0