Skip to content

Instantly share code, notes, and snippets.

@vcpablo
Created February 12, 2019 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vcpablo/d7364e66dd7b55c853f79c4b0de49d72 to your computer and use it in GitHub Desktop.
Save vcpablo/d7364e66dd7b55c853f79c4b0de49d72 to your computer and use it in GitHub Desktop.
Repeat chars single bigger sequences - JavaScript
/**
* 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