An efficient algorithm for finding smallest pangrammatic windows?

前端 未结 2 750
一整个雨季
一整个雨季 2021-01-02 17:31

A pangrammatic window is a substring of a larger piece of text that contains all 26 letters of the alphabet. To quote an example from Wikipedia, given this

2条回答
  •  日久生厌
    2021-01-02 18:01

    For every letter keep track of the recent-most sighting. Whenever you process a letter, update the corresponding sighting index and calculate the range (max-min) of sighting indexes over all letters. Find the location with minimum range.

    Complexity O(n). O(nlog(m)) if you consider alphabet size m.

提交回复
热议问题