Longest substrings without repeating characters: Difference between revisions

Content added Content deleted
(→‎{{header|Phix}}: simplified: use found to hold indices instead of bools.)
(Added AutoHotkey)
Line 7: Line 7:
{{Template:Strings}}
{{Template:Strings}}
<br><br>
<br><br>

=={{header|AutoHotkey}}==
<lang AutoHotkey>LSWRC(str){
found := [], result := [], maxL := 0
if (StrLen(str) = 1)
result[str] := true
else while (StrLen(str) >= 2){
s := str
while StrLen(s) >= maxL{
if !(s ~= "(.).*\1"){
found[s] := true
maxL := maxL < StrLen(s) ? StrLen(s) : maxL
break
}
s := SubStr(s, 1, StrLen(s)-1) ; trim last chr
}
str := SubStr(str, 2) ; trim 1st chr and try again
}
maxL := 0
for str in found
maxL := maxL < StrLen(str) ? StrLen(str) : maxL
for str in found
if (StrLen(str) = maxL)
result[str] := true
return result
}</lang>
Examples:<lang AutoHotkey>db =
(
xyzyabcybdfd
xyzyab
zzz
a
)
for i, line in StrSplit(db, "`n", "`r"){
result := "[", i := 0
for str in LSWRC(line)
result .= str ", "
output .= line "`t> " Trim(result, ", ") "]`n"
}
MsgBox % output
return</lang>
{{out}}
<pre>xyzyabcybdfd > [cybdf, zyabc]
xyzyab > [zyab]
zzz > [z]
a > [a]
</pre>