京东笔试《相似字符串》

输入两个字符串S, T,假如S的子串与T串的格式相同,则认为相似。如:S:ababcb, T:xyx,  则S中aba,bab, bcb与T串相似,输出3.

a = 'ababcb'
b = 'xyx'
def  solve(S, T):
    if len(S) == 0:
        return 0
    a = []
    b = []
    for i in range(0, len(T)):
        if T[i] not in a:
            a.append(T[i])
            b.append([i])
        else:
            j = a.index(T[i])
            b[j].append(i)
    if len(S) == len(T):
        d = []
        for ii in range(0, len(S)):
            if S[ii] not in d:
                d.append(S[ii])
        ans = 0
        f = 0
        t = 0
        for i in range(0, len(S)):
            if len(b[t]) == 1:
                if S[i] == S[b[t][0]]:
                    continue
                else:
                    f = 1
                    break
            else:
                for j in range(0, len(b[t])):
                    if S[i] == S[b[i][j]]:
                        continue
                    else:
                        f = 1
                        break
            if f == 1:
                break
            t = t + 1
        if f == 0:
            ans = ans + 1
        return ans
    else:
        ans = 0
        for i in range(0, len(S) - len(T) + 1):
            tem = S[i:i + len(T)]
            f = 0
            t = 0
            d = []
            for ii in range(0, len(tem)):
                if tem[ii] not in d:
                    d.append(tem[ii])
            for j in range(0, len(d)):
                if len(b[t]) == 1:
                    if tem[j] == tem[b[t][0]]:
                        continue
                    else:
                        f = 1
                        break
                else:
                    for jj in range(0, len(b[t])):
                        if tem[j] == tem[b[t][jj]]:
                            continue
                        else:
                            f = 1
                            break
                if f == 1:
                    break
                t = t + 1

            if f == 0:
                ans = ans + 1
        return ans

d = solve(a, b)

print d

 

s = input()
t = input()
c = 0 
sl = [i for i in s]
tl = [i for i in t]
ls = len(s)
lt = len(t)
set_t = set(tl)
lstl = len(set(tl))

for i in range(ls-lt+1):
    test_s = sl[i : i+lt]

    if len(set(test_s)) != lstl:
        continue
    else:
        z = list(zip(tl, test_s))
        c_t = 0
        for each in set_t:
            
            tmp = list(filter(lambda x:x[0]==each, z))
            if len(set(tmp)) != 1:
                break
            c_t += 1

        if c_t == lstl:
            c += 1 
print(c)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT远征军

谢谢各位鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值