r/dailyprogrammer • 1 2 • Jun 10 '13

[Easy] Longest Two-Character Sub-String

(Easy): Longest Two-Character Sub-String

This programming challenge is a classic interview question for software engineers: given a string, find the longest sub-string that contains, at most, two characters.

Author: /u/Regul

Formal Inputs & Outputs

Input Description

Through standard console input, you will be given a string to search, which only contains lower-case alphabet letters.

Output Description

Simply print the longest sub-string of the given string that contains, at most, two unique characters. If you find multiple sub-strings that match the description, print the last sub-string (furthest to the right).

Sample Inputs & Outputs

Sample Inputs

abbccc
abcabcabcabccc
qwertyytrewq

Sample Outputs

bbccc
bccc
tyyt
62 Upvotes

129 comments sorted by

View all comments

Show parent comment

1

u/MrDerk Jun 10 '13 edited Jun 11 '13

Thanks. I don't know of a cleaner way to find unique members than set. You can do it with dict but that's a bit more cumbersome (and much less legible).

There's also some short circuiting that could be done (returning a substring of length n once you're within n characters of the end of the input string in the i loop, for example), but I'm not sure that would save you much unless you were running this hundreds of thousands of times or on particularly long strings.