Longest substrings without repeating characters: Difference between revisions

Content added Content deleted
(Add Modula-2)
(Add BCPL)
Line 88: Line 88:
a > [a]
a > [a]
</pre>
</pre>

=={{header|BCPL}}==
<lang bcpl>get "libhdr"

// Fills up 'v' with words where w%0 is the start and w%1 is the end
// of each longest substring
let lswrc(s, v) = valof
$( let seen = vec 255
let start = 1 and i = 1 and maxStart = 1 and maxEnd = 1 and n = 0
for i=0 to 255 do i!seen := false
while i <= s%0
$( test (s%i)!seen
while (s%i)!seen
$( (s%start)!seen := false
start := start + 1
$)
or
$( (s%i)!seen := true
if i - start >= maxEnd - maxStart
$( maxStart := start
maxEnd := i
while n>0 & (v+n-1)%1 - (v+n-1)%0 < i-start
do n := n-1
(v+n)%0 := start
(v+n)%1 := i
n := n+1
$)
i := i + 1
$)
$)
resultis n
$)

let substr(s, start, end, buf) = valof
$( buf%0 := end - start + 1
for i = start to end do
buf%(i-start+1) := s%i
resultis buf
$)

let example(s) be
$( let v = vec 32 and b = vec 32
let n = lswrc(s, v)
writef("Original string: '%s'*N", s)
writes("Longest substrings: ")
test n=0 do
writes("<empty>")
or for i = 0 to n-1 do
writef("'%s' ", substr(s, (v+i)%0, (v+i)%1, b))
wrch('*N')
$)

let start() be
$( example("xyzyabcybdfd")
example("xyzyab")
example("zzzzz")
example("a")
example("")
$)</lang>
{{out}}
<pre>Original string: 'xyzyabcybdfd'
Longest substrings: 'zyabc' 'cybdf'
Original string: 'xyzyab'
Longest substrings: 'zyab'
Original string: 'zzzzz'
Longest substrings: 'z' 'z' 'z' 'z' 'z'
Original string: 'a'
Longest substrings: 'a'
Original string: ''
Longest substrings: <empty></pre>


=={{header|C++}}==
=={{header|C++}}==