Created
February 12, 2019 21:11
-
-
Save vcpablo/d7364e66dd7b55c853f79c4b0de49d72 to your computer and use it in GitHub Desktop.
Repeat chars single bigger sequences - JavaScript
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
/** | |
* Returns the sequences of repeated subsequence chars | |
* Example 1: 'Niiiiiiiceee' will return ['iiiiiii', 'eee'] | |
* Example 2: 'NiiiiiiiceeeNiccceeeeeeeeeee' will return ['iiiiiii', 'ccc', 'eeeeeeeeeee'] | |
* | |
* Notice that in 'Example 2', the 'e'char sequence that is returned is the second one, because it's bigger than the first one. | |
*/ | |
const singleSequencesEqualChars = function(word) { | |
let counter, | |
checkedChars = [], | |
result = {}; | |
if(word.length == 0) { | |
return null; | |
} | |
result = {}; | |
for(let i = 0; i < word.length; i++) { | |
counter = 0; | |
for(let j = i; j < word.length; j++) { | |
let currentChar = word.charAt(i); | |
let nextChar = word.charAt(j); | |
let charMissing = checkedChars.indexOf(currentChar) === -1; | |
if(currentChar === nextChar) { | |
counter++; | |
let foundBiggerSequence = (result[currentChar] || '').length < counter; | |
if(counter > 1 && (charMissing || foundBiggerSequence)) { | |
result[currentChar] = ''.padStart(counter, currentChar); | |
} | |
} else { | |
checkedChars.push(currentChar); | |
break; | |
} | |
} | |
} | |
const values = Object.values(result); | |
console.log(values); | |
return values.length === 0 ? null : values; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment