Last active
April 26, 2021 17:44
-
-
Save mkstix6/02da99454632cb7435e3d36dbd518144 to your computer and use it in GitHub Desktop.
Cassidoo challenge #193 20210426
This file contains hidden or 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
/** | |
* Given a string of brackets, return a rotation | |
* of those brackets that is balanced. The numbers | |
* of opening and closing brackets will always be | |
* equal, so [ or ][] won't be given as inputs. | |
* | |
* Example: | |
* $ rotateBrackets(']][][[') | |
* $ '[[]][]' // First rotation yields '[]][]['.Second one yields '[[]][]'. | |
* | |
* This was hard to read and understand so I've elaborated myself: | |
* Starting point: ']][][[' ❌ is not balanced, so rotate again. | |
* First rotation yields: '[]][][' ❌ is not balanced, so rotate again. | |
* Second rotation yields:'[[]][]' ✅ is balanced …return as answer. | |
*/ | |
const rotateString = (string) => `${string.slice(-1)}${string.slice(0, -1)}`; | |
const rotateBrackets = (brackets) => { | |
let rotated = brackets; | |
do { | |
rotated = rotateString(rotated); | |
} while (!isBalanced(rotated)); | |
return rotated; | |
}; | |
const isBalanced = (brackets) => { | |
let theBalance = 0; | |
for (let character of brackets) { | |
if (character === "[") theBalance++; | |
if (character === "]") theBalance--; | |
if (theBalance < 0) return false; | |
} | |
return theBalance === 0; | |
}; | |
// Simple test | |
console.log( | |
rotateBrackets("]][][[") === "[[]][]" | |
? `✅ Code works.` | |
: `❌ Code doesn't work.` | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment