Given a string as input find the longest contiguous substring that does not have any character twice or more. If there are multiple such substrings you may output either. You may assume that the input is on the printable ASCII range if you wish.
Scoring
Answers will first be ranked by the length of their own longest non-repeating substring, and then by their total length. Lower scores will be better for both criteria. Depending on the language this will probably feel like a code-golf challenge with a source restriction.
Triviality
In some languages achieving a score of 1, x (lenguage) or 2, x (Brain-flak and other turing tarpits) is pretty easy, however there are other languages in which minimizing the longest non-repeating substring is a challenge. I had a lot of fun getting a score of 2 in Haskell, so I encourage you to seek out languages where this task is fun.
Test cases
"Good morning, Green orb!" -> "ing, Gre"
"fffffffffff" -> "f"
"oiiiiioiiii" -> "io", "oi"
"1234567890" -> "1234567890"
"11122324455" -> "324"
Scoring submission
You can score your programs using the following snippet:
input.addEventListener("input", change);
// note: intentionally verbose :)
function isUnique(str) {
var maxCount = 0;
var counts = {};
for(var i = 0; i < str.length; i++) {
var c = str.charAt(i);
counts[c] |= 0;
counts[c]++;
if(maxCount < counts[c]) {
maxCount = counts[c];
}
}
return maxCount <= 1;
}
function maximizeSubstring(str, pred, cost) {
var mostExpensive = -1;
// iterate over substrings
function iterate(start, end) {
var slice = str.slice(start, end);
if(pred(slice)) {
var profit = cost(slice);
if(profit > mostExpensive) {
mostExpensive = profit;
}
}
end++;
if(start >= str.length) {
return;
}
else if(end > str.length) {
start++;
iterate(start, start);
}
else {
iterate(start, end);
}
}
iterate(0, 0);
return mostExpensive;
}
function size(x) {
return x.length;
}
function longestNonRepeatingSize(str) {
return maximizeSubstring(str, isUnique, size);
}
function change() {
var code = input.value;
output.value = "Your score is: " + longestNonRepeatingSize(code);
}
change();
* {
font-family: monospace;
}
Input code here:
<br>
<textarea id="input"></textarea>
<br>
<textarea id="output"></textarea>
11122324455
Jonathan Allan realized that my first revision didn't handle it correctly. \$\endgroup\$11122
ocurrs after324
, but gets deduplicated to12
. \$\endgroup\$