Created
March 28, 2021 15:09
-
-
Save rafiq/aefa84ff5ef9a4f02f8b67264d6abdf9 to your computer and use it in GitHub Desktop.
#1 Strings: Find The Longest Substring and Required Data. 6 kyu Code Wars JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
We have to find the longest substring of identical characters in a very long string. | |
Let's see an example: | |
s1 = "1111aa994bbbb1111AAAAAFF?<mmMaaaaaaaaaaaaaaaaaaaaaaaaabf" | |
The longest substring in s1 is "aaaaaaaaaaaaaaaaaaaaaaaaa" having a length of 25, made of character, "a", with its corresponding ascii code equals to "97", and having its starting index 29 and the ending one 53. | |
We express the results using an array in the following order: ["97", 25, [29,53]] | |
The symbols '!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~' that a string may have will be considered as noise, so they cannot be a solution even though the substring, made of one of them, is the longest of all. In other words, the allowed characters may be the uppercase or lower letters of the English alphabet or the decimal digits ('0123456789') | |
Let's see an example to show what happens if we have non alphanumeric symbols. | |
s2 = "1111aa994bbbb1111AAAAAFF????????????????????????????" The longest substring is "AAAAA" so the result for it is: | |
['65', 5, [17, 21]] | |
If there are two or more substrings with the maximum length value, it will be chosen the one that starts first, from left to right. | |
Make an agile code that may output an array like the one shown above when it receives a huge string. | |
Features of the random tests: | |
number of tests = 210 | |
length of the strings up to a bit more than: | |
10.000.000 (python and javascript) | |
6.000.000 (ruby) | |
*/ | |
function findLongestSubstr(s) { | |
if (s.length === 0) return ""; | |
let maxStrArrray = [s[0]]; | |
let maxPos = 0; | |
let current = 1; | |
for (let i = 1; i < s.length; i++) { | |
if (s[i] === s[i - 1] && i !== s.length - 1) { | |
current++; | |
} else if (s[i] !== s[i - 1]) { | |
maxStrArrray.push(s.substr((i - current),current)); | |
current = 1; | |
} else if (i === s.length - 1) { | |
current++; | |
maxStrArrray.push(s.substr((i + 1 - current),current)); | |
current = 1; | |
} | |
} | |
let maxStrArrrayNums = maxStrArrray.filter(el => { | |
return (parseInt(el) == el) | |
}) | |
let maxStrArrrayLetters = maxStrArrray.filter(el => { | |
return el.toLowerCase() >= "a" && el.toLowerCase() <= "z" | |
}) | |
maxStrArrray = maxStrArrrayLetters.concat(maxStrArrrayNums); | |
let lengthsArray = maxStrArrray.map(x => x.length); | |
maxPos = s.indexOf(maxStrArrray[lengthsArray.indexOf(Math.max(...lengthsArray))]); | |
let maxLetter = maxStrArrray[lengthsArray.indexOf(Math.max(...lengthsArray))][0].charCodeAt(); | |
let maxLength = Math.max(...lengthsArray) | |
return [`${maxLetter}`, +`${maxLength}`,[+`${maxPos}`, +`${maxPos + maxLength - 1}`]] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment