Longest substrings without repeating characters: Difference between revisions
Longest substrings without repeating characters (view source)
Revision as of 20:50, 27 August 2022
, 1 year agosyntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 11:
{{trans|Python: Some optimisation}}
<
V max_subs = [s[0.<1]] * 0
V mx = 0
Line 29:
V arr = [1, 2, 3, 4, 1, 2, 5, 6, 1, 7, 8, 1, 0]
print(arr‘ => ’longest_substring2(arr))</
{{out}}
Line 42:
=={{header|Action!}}==
<
BYTE ARRAY tab(256)
BYTE i
Line 106:
Test("thisisastringtest")
Test("")
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Longest_substrings_without_repeating_characters.png Screenshot from Atari 8-bit computer]
Line 130:
=={{header|Ada}}==
<
procedure Longest_Substring is
Line 186:
Test ("thisisastringtest");
Test ("zzzzz");
end Longest_Substring;</
{{out}}
<pre>
Line 202:
=={{header|AutoHotkey}}==
<
found := [], result := [], maxL := 0
if (StrLen(str) = 1)
Line 225:
result[str] := true
return result
}</
Examples:<
(
xyzyabcybdfd
Line 241:
}
MsgBox % output
return</
{{out}}
<pre>xyzyabcybdfd > [cybdf, zyabc]
Line 250:
=={{header|BCPL}}==
<
// Fills up 'v' with words where w%0 is the start and w%1 is the end
Line 308:
example("a")
example("")
$)</
{{out}}
<pre>Original string: 'xyzyabcybdfd'
Line 322:
=={{header|C}}==
<
#include <stdlib.h>
#include <string.h>
Line 374:
return 0;
}</
{{out}}
<pre>Original string: "xyzyabcybdfd"
Line 388:
=={{header|C++}}==
<
#include <fstream>
#include <set>
Line 455:
test1();
test2("unixdict.txt");
}</
{{out}}
Line 483:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
: unique-substrings ( seq n -- newseq )
Line 497:
"Longest substrings without repeating characters:" print
{ "xyzyabcybdfd" "xyzyab" "zzzzz" "a" "" } [ test ] each</
{{out}}
<pre>
Line 509:
=={{header|FreeBASIC}}==
<
function nrls( t as string ) as string
Line 539:
print ">";nrls("abcdefghijklmnopqrstuvwxyz");"<"
print ">";nrls("aaa aeiou uuu");"<"
</syntaxhighlight>
{{out}}<pre>
>blishmentar<
Line 551:
=={{header|Go}}==
{{trans|Wren}}
<
import "fmt"
Line 615:
fmt.Printf("Length = %d\n\n", len(longest[0]))
}
}</
{{out}}
Line 643:
=={{header|J}}==
Implementation:<
c=. #y
while. ss=. c ]\ y do.
Line 650:
c=. c-1
end.
}}</
Examples:
<
zyabc
cybdf
Line 669:
ringtes
longnorep 'Thequickbrownfoxjumpsoverthelazydog'
Thequickbrownf</
This also works on sequences of numbers (though the character representation of the sequences of numbers may contain repeated characters, the represented numbers will not contain repetitions):
<
3 4 1 2 5 6
2 5 6 1 7 8</
=={{header|jq}}==
Line 681:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<
def alluniquehead:
length as $n
Line 711:
end)
| .ans;
</syntaxhighlight>
'''Test Cases'''
<
"xyzyab",
"zzzzz",
Line 719:
""
| "\"\(.)\" => \"\(maximal_substring_with_distinct_characters)\""
</syntaxhighlight>
{{out}}
<pre>
Line 732:
=={{header|Julia}}==
Works on any array, treating a character string as a character array.
<
len = length(arr)
if len > 1
Line 756:
println("\"$s\" => ", uarray)
end
</
<pre>
"xyzyabcybdfd" => ["zyabc", "cybdf"]
Line 768:
=={{header|Modula-2}}==
<
FROM InOut IMPORT WriteString, WriteLn;
FROM Strings IMPORT Length, Copy;
Line 844:
Example("a");
Example("");
END LSWRC.</
{{out}}
<pre>Original string: xyzyabcybdfd
Line 859:
=={{header|Nim}}==
This version converts strings to sequences of runes.
<
type Runes = seq[Rune]
Line 889:
echo "\nLongest substrings in concatenated list of words from “unixdict.txt”: ",
longestSubstrings(toSeq("unixdict.txt".lines).join())</
{{out}}
Line 903:
=={{header|Perl}}==
Gets the same answer that raku does when run against unixdict.txt
<
use strict; # Longest_substrings_without_repeating_characters
Line 914:
length $+ >= $#sub and ++$sub[length $+]{$+} while s/.*(.)(.*\K\1.*)|(.+)//s;
printf "%20s -> %s\n", $string, join ' ', sort keys %{ pop @sub };
}</
{{out}}
<pre>
Line 930:
Should be exponentially faster than collecting all possible substrings and eliminating those with duplicate characters or too short.<br>
It will however collect duplicates (when long enough) before weeding them out at the end.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 973:
<span style="color: #0000FF;">?</span><span style="color: #000000;">longest_substrings</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"a"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">longest_substrings</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 986:
=={{header|Python}}==
The following algorithm works but is not terribly efficient for long strings.
<
substr = [s[x:y] for x in range(len(s)) for y in range(x+1, len(s) + 1)]
no_reps = []
Line 999:
for s in ["xyzyabcybdfd", "xyzyab", "zzzzz", "a", "α⊆϶α϶", "",
[1, 2, 3, 4, 1, 2, 5, 6, 1, 7, 8, 1, 0]]:
print(f"{s} => {longest_substring(s)}")</
{{out}}
Line 1,012:
===Python: Some optimisation===
The following algorithm only accrues the longest so far.
<
max_subs, mx = [], 0
for x in range(len(s)):
Line 1,022:
else:
max_subs, mx = [sub], y - x
return max_subs</
{{out}}
Line 1,033:
Not going to bother handling arrays since an array is not a string, and the task description '''specifically''' says 'Given a string'.
<syntaxhighlight lang="raku"
sub longest ($string) {
Line 1,059:
# check a file
slurp 'unixdict.txt';</
{{out}}
<pre>Original string: xyzyabcybdfd
Line 1,101:
=={{header|REXX}}==
<
parse arg $ /*obtain optional argument from the CL.*/
if $=='' | $=="," then $= 'xyzyabcybdfd' /*Not specified? Then use the default.*/
Line 1,123:
OKx: procedure; parse arg y; do r=1 for length(y)-1 /*look for duplicate chars.*/
if pos(substr(y, r, 1), y, r+1)>0 then return 0
end /*r*/; return 1</
{{out|output|text= when using the default input:}}
<pre>
Line 1,130:
=={{header|Ring}}==
<
see "working..." + nl
see "Longest substrings without repeating characters are:" + nl
Line 1,181:
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,193:
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
function nrls(byval s1)
dim i,x
Line 1,240:
test "aa"
test "abdefghij"
</syntaxhighlight>
{{out}}
<pre>
Line 1,262:
=={{header|Vlang}}==
{{trans|go}}
<
n := s.len
if n == 0 {
Line 1,322:
println("Length = ${longest[0].len}\n")
}
}</
{{out}}
<pre>Same as Wren entry</pre>
Line 1,328:
=={{header|Wren}}==
{{libheader|Wren-seq}}
<
var substrings = Fn.new { |s|
Line 1,361:
System.print(longest)
System.print("Length = %(longest[0].count)\n")
}</
{{out}}
|