r/dailyprogrammer • u/nint22 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
qwertyytrewqSample Outputs
bbccc
bccc
tyyt
View all comments
Show parent comment
2
u/[deleted] Jun 19 '13 edited Jun 19 '13
You're right. You don't understand my algorithm because it's wrong.
cur_substr.count was definitely the wrong function to use since it counts all the characters in the current substring! The algorithm was supposed to track of the beginning and end (i, j) of each substring and then moves i to j minus the number of identical consecutive characters when a third unique character is detected. j = len(max_substr) shouldn't be there and has been deleted...