6 kyu

Longest 2-character substring

152 of 329JDeBolt

Description:

Find the longest substring within a string that contains at most 2 unique characters.

substring("a") => "a"
substring("aaa") => "aaa"
substring("abacd") => "aba"
substring("abacddcd") => "cddcd"
substring("cefageaacceaccacca") => "accacca"

This function will take alphanumeric characters as input.

In cases where there could be more than one correct answer, the first string occurrence should be used. For example, substring('abc') should return 'ab' instead of 'bc'.

Although there are O(N^2) solutions to this problem, you should try to solve this problem in O(N) time. Tests may pass for O(N^2) solutions but, this is not guaranteed.

This question is much harder than some of the other substring questions. It's easy to think that you have a solution and then get hung up on the implementation.

Algorithms

Stats:

CreatedAug 1, 2015
PublishedAug 1, 2015
Warriors Trained906
Total Skips39
Total Code Submissions2691
Total Times Completed329
JavaScript Completions152
Python Completions188
Total Stars48
% of votes with a positive feedback rating94% of 110
Total "Very Satisfied" Votes99
Total "Somewhat Satisfied" Votes9
Total "Not Satisfied" Votes2
Ad
Contributors
  • JDeBolt Avatar
  • CIS 122 Avatar
  • Blind4Basics Avatar
Ad