Skip to content

Instantly share code, notes, and snippets.

@bekliev
Last active January 21, 2020 23:25
Show Gist options
  • Save bekliev/84c553c2ad385260f37c2280da5aede7 to your computer and use it in GitHub Desktop.
Save bekliev/84c553c2ad385260f37c2280da5aede7 to your computer and use it in GitHub Desktop.
/*
* Try to copy&paste to your browser's console
* OR open this image: https://user-images.githubusercontent.com/4066435/72851941-f7ec8f00-3ca4-11ea-9840-29511325a772.png
*/
function solution(str, maxEquals = 2) {
let result = ''
let equals = 0
for (const i in str) {
const prev = str.charAt(i - 1)
const curr = str.charAt(i)
if (prev === curr) equals++
else equals = 0
if (equals >= maxEquals) continue
result += curr
}
return result
}
solveAndLog('eedaaad')
solveAndLog('xxxtxxx')
solveAndLog('uuuuxaaaaxuuu')
solveAndLog('abbbbbcccddddeeee', 1)
solveAndLog('')
function solveAndLog(str, maxEquals) {
console.log({got: str, result: solution(str, maxEquals)})
}
@bekliev
Copy link
Author

bekliev commented Jan 21, 2020

Here's the result of the code above:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment