Last active
August 10, 2021 20:01
-
-
Save mflorida/a2c3f2181efedca97f6d849700a9e0e2 to your computer and use it in GitHub Desktop.
Remove '100' by splitting and re-joining string until empty or equal to 100 ('yes'), or end result equals itself ('no').
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
function writeOutput(inputData) { | |
// Start writing code here to consume input, and return result. | |
// split lines of input string into an array | |
const inputParts = inputData.split('\n'); | |
// get value of first element to serve as line count | |
const lineCount = Number(inputParts[0]); | |
// update this after we have a result to minimize time complexity (no nested loops) | |
let inputIndex = 1; | |
function split100(input) { | |
return input.split('100').join(''); | |
} | |
let testData = split100(inputParts[inputIndex]); | |
let prevData = null; | |
// testData is new string with 100s removed | |
while (inputIndex <= lineCount) { | |
// checking for 100 eliminatea one iteration | |
if (testData === '100' || testData === '') { | |
console.log('yes'); | |
prevData = null; | |
} | |
else if (testData === prevData) { | |
console.log('no'); | |
prevData = null; | |
} | |
else { | |
prevData = testData; | |
testData = split100(testData); | |
} | |
if (prevData === null) { | |
// move on to the next line if done here | |
inputIndex += 1; | |
testData = split100(inputParts[inputIndex] || '') | |
} | |
} | |
return '' | |
} | |
const inputData = '' + | |
'8\n' + | |
'101000\n' + // yes | |
'1010001\n' + // no | |
'1001\n' + // no | |
'10010\n' + // no | |
'100110110000000\n' + // yes | |
'1001001100100100\n' + // no | |
'100110111000000000\n' + // yes | |
'101000101000' + // yes | |
''; | |
writeOutput(inputData); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment