Given a word and a text, we need to return the occurrences of anagrams

后端 未结 5 2005
囚心锁ツ
囚心锁ツ 2020-12-17 20:58

Given a word and a text, return the count of the occurrences of anagrams of the word in the text. For eg. word is “for” and the text is “forxxorfxdofr”, anagrams of “for” wi

5条回答
  •  醉梦人生
    2020-12-17 21:47

    o(n) solution in Python 
    

    def check(s1,s2):

    function takes in s1 as the text and s2 as the text to be checked from here for
    
    
    c=0
    n=len(s2)
    ck=sorted(s2)
    mk=''.join(ck)
    
    
     this loop will pick from s till the length of s2 that is 'for'
    
    
    
    for i,item in enumerate(s1):
        if s1[i] in mk:
            p=s1[i:i+n]
            jk=sorted(p)
            er=''.join(jk)
    
    now just comparing the both sorted strings if they are equal then they were anagram 
    
            if er == mk:
                c+=1
    return c
    

提交回复
热议问题