Can regular expressions be used to match strings with similar shapes in Python?

JD.com "s pen question
input sample:
string A: ababcb
string B: xyx
the substring aba,bab,bcb of substring An is equivalent to the shape of string B. We are asking to find out how many substrings in An are equivalent to string B.
(obviously, the length of string An is greater than or equal to the length of string B, in the case of equal sign, for example: string A br abcc, string B:xyaa) the import re, in the < string > prompt should be able to be implemented with regularity?


python3

-sharp!/usr/bin/env python
-sharp -*- coding: utf-8 -*-
-sharp @File  : 02.py
-sharp @Author: huifer
-sharp @Date  : 2018/9/10

def indexOfEm(str_b):
    -sharp 
    set_b = set(str_b)
    en_numb = dict()
    for i, e in enumerate(set_b):
        en_numb[e] = i
    return en_numb


def strToNumb(str_b):
    -sharp  xyx 
    en_numb = indexOfEm(str_b)
    new_b = list(str_b)
    new_str = [en_numb.get(i) for i in new_b]
    return new_str


def splitJoinStr(str_a, str_b):
    -sharp  b
    str_a = list(str_a)
    str_b = list(str_b)
    split_len = len(str_b)

    res = list()

    for i in range(str_a.__len__()):
        a = str_a[i:split_len + i]
        if a.__len__() == split_len:
            res.append(a)

    return res

def xtoy(str_b):
    -sharp  
    a = str_b.replace("x","z").replace("y","x").replace("z","y")
    return a



if __name__ == '__main__':
    str_a = "ababcb"
    str_b = "xyx"

    str_b_2 = xtoy(str_b)


    -sharp 
    standard = strToNumb(str_b)
    standard_2 = strToNumb(str_b_2)
    print("", standard)
    print("", standard_2)

    -sharp 
    split_str = splitJoinStr(str_a, str_b)
    for i in split_str:
        -sharp 
        da = strToNumb(i)
        -sharp print(da)
        if da == standard or da ==standard_2:
            print("",i)

I don't know if the result is in line with your idea

Menu