Most common substring of length X

前端 未结 8 2166
没有蜡笔的小新
没有蜡笔的小新 2021-02-09 13:49

I have a string s and I want to search for the substring of length X that occurs most often in s. Overlapping substrings are allowed.

For example, if s=\"aoaoa\" and X=3

8条回答
  •  走了就别回头了
    2021-02-09 14:25

    Naive solution in Python

    from collections import defaultdict
    from operator    import itemgetter
    
    def naive(s, X):
        freq = defaultdict(int)
        for i in range(len(s) - X + 1):
            freq[s[i:i+X]] += 1
        return max(freq.iteritems(), key=itemgetter(1))
    
    print naive("aoaoa", 3)
    # -> ('aoa', 2)
    

    In plain English

    1. Create mapping: substring of length X -> how many times it occurs in the s string

      for i in range(len(s) - X + 1):
          freq[s[i:i+X]] += 1
      
    2. Find a pair in the mapping with the largest second item (frequency)

      max(freq.iteritems(), key=itemgetter(1))
      

提交回复
热议问题