Most common substring of length X

前端 未结 8 2390
没有蜡笔的小新
没有蜡笔的小新 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:16

    It should be O(n*m) where m is the average length of a string in the list. For very small values of m then the algorithm will approach O(n)

    • Build a hashtable of counts for each string length
    • Iterate over your collection of strings, updating the hashtable accordingly, storing the current most prevelant number as an integer variable separate from the hashtable
    • done.

提交回复
热议问题