Longest substrings without repeating characters: Difference between revisions
Content deleted Content added
No edit summary |
Added Wren |
||
Line 64: | Line 64: | ||
len = 5 : cybdf |
len = 5 : cybdf |
||
done... |
done... |
||
</pre> |
|||
=={{header|Wren}}== |
|||
<lang ecmascript>import "/seq" for Lst |
|||
var substrings = Fn.new { |s| |
|||
var n = s.count |
|||
if (n == 0) return [""] |
|||
var ss = [] |
|||
for (i in 0...n) { |
|||
for (len in 1..n-i) ss.add(s[i...i+len]) |
|||
} |
|||
return ss |
|||
} |
|||
System.print("The longest substring(s) of the following without repeating characters are:\n") |
|||
var strs = ["xyzyabcybdfd", "xyzyab", "zzzzz", "a", "", ] |
|||
for (s in strs) { |
|||
var longest = [] |
|||
var longestLen = 0 |
|||
for (ss in substrings.call(s)) { |
|||
if (Lst.distinct(ss.toList).count == ss.count) { |
|||
if (ss.count >= longestLen) { |
|||
if (ss.count > longestLen) { |
|||
longest.clear() |
|||
longestLen = ss.count |
|||
} |
|||
longest.add(ss.join()) |
|||
} |
|||
} |
|||
} |
|||
longest = Lst.distinct(longest) |
|||
System.print("String = '%(s)'") |
|||
System.print(longest) |
|||
System.print("Length = %(longest[0].count)\n") |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
The longest substring(s) of the following without repeating characters are: |
|||
String = 'xyzyabcybdfd' |
|||
[zyabc, cybdf] |
|||
Length = 5 |
|||
String = 'xyzyab' |
|||
[zyab] |
|||
Length = 4 |
|||
String = 'zzzzz' |
|||
[z] |
|||
Length = 1 |
|||
String = 'a' |
|||
[a] |
|||
Length = 1 |
|||
String = '' |
|||
[] |
|||
Length = 0 |
|||
</pre> |
</pre> |
Revision as of 09:14, 27 May 2021
Longest substrings without repeating characters is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
- Given a string find the longest substring without repeating characters.
Ring
<lang ring> see "working..." + nl see "Longest substrings without repeating characters are:" + nl str = "xyzyabcybdfd" see "Input: " + str + nl lenStr = len(str) strOk = [] lenOk = 0
for n = 1 to lenStr
for m = 1 to lenStr-n+1 temp = substr(str,n,m) add(strOk,temp) next
next
for n = len(strOk) to 1 step -1
if len(strOK[n]) = 1 del(strOK,n) ok
next
for n = 1 to len(strOK)
for m = 1 to len(strOK)-1 if len(strOK[m+1]) > len(strOK[m]) temp = strOK[m] strOK[m] = strOK[m+1] strOK[m+1] = temp ok next
next
for n = 1 to len(strOK)
flag = 1 for m = 1 to len(strOK[n]) for p = m+1 to len(strOK[n]) if strOK[n][m] = strOK[n][p] flag = 0 exit ok next next if flag = 1 if len(strOK[n]) >= lenOk see "len = " + len(strOK[n]) + " : " + strOK[n] + nl lenOK = len(strOK[n]) ok ok
next
see "done..." + nl </lang>
- Output:
working... Longest substrings without repeating characters are: Input: xyzyabcybdfd len = 5 : zyabc len = 5 : cybdf done...
Wren
<lang ecmascript>import "/seq" for Lst
var substrings = Fn.new { |s|
var n = s.count if (n == 0) return [""] var ss = [] for (i in 0...n) { for (len in 1..n-i) ss.add(s[i...i+len]) } return ss
}
System.print("The longest substring(s) of the following without repeating characters are:\n") var strs = ["xyzyabcybdfd", "xyzyab", "zzzzz", "a", "", ] for (s in strs) {
var longest = [] var longestLen = 0 for (ss in substrings.call(s)) { if (Lst.distinct(ss.toList).count == ss.count) { if (ss.count >= longestLen) { if (ss.count > longestLen) { longest.clear() longestLen = ss.count } longest.add(ss.join()) } } }
longest = Lst.distinct(longest) System.print("String = '%(s)'") System.print(longest) System.print("Length = %(longest[0].count)\n")
}</lang>
- Output:
The longest substring(s) of the following without repeating characters are: String = 'xyzyabcybdfd' [zyabc, cybdf] Length = 5 String = 'xyzyab' [zyab] Length = 4 String = 'zzzzz' [z] Length = 1 String = 'a' [a] Length = 1 String = '' [] Length = 0