Shortest row digest

[Description] If a string of type char is specified, find the shortest digest, which is defined as: the shortest substring containing all the characters in the source string.

[Example]

A = "aaabedacd"

B = "bedac" is the answer.

[My decision]

  • Define an integer table with 256 elements, which is used to record the time that each type of character appeared in the current substring.

  • Scan the entire line, statistics on common character types in a given line, using the table above.

  • Use two pointers, the beginning, the end, which initially indicate the beginning and (beginning + 1) of this line. Current character types: 1.

  • Expand the substring [start, end) at the end until it contains all kinds of characters. If possible, update the shortest digest.

  • The substring contract [start, end] at the beginning, one character each time, try to restore its digest property, if necessary, in step 4.

The cost of time is O (n), and the additional cost of space is constant.

Any better solution without extra space?

+3
source share
3 answers

This works very poorly when you think that the characters are not really limited to 256; Unicode is closer to 2 ^ 32 code points; and if you try what you plan on using in the UTF-8 string, it will explode. To a large extent.

A better approach would be to use a digest algorithm such as MD5 or FNV, or do what you do, but rather with a sparse array linked list; , , , , , UTF-8, .

EDIT:

: "På japansk heter regn '雨'."

+2

, . : "baaabedacdc". - "bedac", , "e" ( 1), , "e" , ( 1), "e" .

, .

+2

, , , ;):

1. (, 26 ), 26 , . , elem [0] = true, a, elem [1] = true, b ..

2. , , elem [x] = true. "abcde" = 5.

3. , 5, 2.

-1

Source: https://habr.com/ru/post/1735203/


All Articles