Last active
October 23, 2020 08:41
-
-
Save max-lt/d2757767b0020b761840a55b63c5a4ef to your computer and use it in GitHub Desktop.
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
/** | |
* @type [[[number], [[string]]]] | |
*/ | |
const tests = [ | |
[[0, 1, 0], [['tea', 'aba', 'obj'], ['eat', 'aaa', 'job']]], | |
[[3], [['zzz'], ['aaa']]], | |
[[0], [['zzx'], ['xzz']]], | |
[[1, 1], [['zzx', 'xza'], ['xza', 'zzx']]], | |
[[0, 1], [['azx', 'xza'], ['xza', 'zzx']]], | |
[[-1, 0], [['azzz', 'xza'], ['xza', 'zax']]], | |
[[1, 1], [['(((a+', ')))b-'], ['+b(((', ')()b-']]], | |
]; | |
/** | |
* Complete the 'getMinimumDifference' function below. | |
* | |
* The function is expected to return an INTEGER_ARRAY. | |
* The function accepts following parameters: | |
* 1. STRING_ARRAY a | |
* 2. STRING_ARRAY b | |
* | |
* @param {string} a | |
* @param {string} b | |
* @returns [number] | |
*/ | |
function getMinimumDifference(a, b) { | |
// Sanity check: ensuring inputs length | |
if (a.length !== b.length) { | |
throw new Error('Input arrays have different lengths'); | |
} | |
/** @type [number] */ | |
const res = new Array(a.length).fill(-1); | |
for (let i = 0; i < a.length; i++) { | |
const A = a[i]; | |
const B = b[i]; | |
// Cannot be anagrams | |
if (A.length !== B.length) { | |
continue; | |
} | |
if (A.length > 10e4) { | |
continue; | |
} | |
// Split and sort compared strings | |
const charsA = a[i].split('').sort(); | |
const charsB = b[i].split('').sort(); | |
// Test if chains are anagrams | |
if (charsA.join('') === charsB.join('')) { | |
res[i] = 0; | |
continue; | |
} | |
let diff = 0; | |
for (let j = 0; j < charsA.length; j++) { | |
const bIndex = charsB.indexOf(charsA[j]); | |
if (bIndex === -1) { | |
diff++; | |
} | |
else { | |
charsB[bIndex] = null; | |
} | |
} | |
res[i] = diff; | |
} | |
return res; | |
} | |
tests.forEach(([expect, test], index) => { | |
const res = getMinimumDifference(test[0], test[1]) | |
if (res.join(', ') === expect.join(', ')) { | |
console.log(`Test ${index}: OK`) | |
} else { | |
console.log(`Test ${index} failed: expected ${expect} got ${res}`) | |
} | |
}); |
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
/** | |
* @type [[number, string]] | |
*/ | |
const tests = [ | |
[2, '))'], | |
[2, '(('], | |
[4, '))(('], | |
[0, '()'], | |
[2, ')('], | |
[1, ')()'], | |
[5, ')())))('], | |
[564, '()))))))))))))))))))))))()()))()))))))))()))))))()))()))))(()))))))))))))()))))))(()))))))))()()))))))))))))()))))(())()))))))(()))))()))))))()))()())))())))))))))))()))())(()()())()()())))))()))))())()))()))))))))))))))()())))()))))()))))))()))())()))())))(()))()))))))))())))())))(())()))))()((()))))))((((()())())())(())))))())())))))))())))))()(()))))()))))())))))()())())()))()))))))))()))))))))))()))))())))))(((()))))()))((())))())))))))())))()()())())))))())))())())))))(())())))))))())))()()))))))))))))(())())())))((()))))))(())))()())))()))))(())))(())))))))))))))(())))(())()))))(()))())())))))))()())(()(())())))))))))))))))))))))))((()())))())))())))((()())))()))())()))))())()())))))))))))(()))))))))))))))()))))))()))))))))))))))))(()(()))(()))()))))))()))()()))))))))))()))())()))))())))()()()))()))))(())))))))))))))()()))))(())))()))))))()))()())()))())()())())))()()(()())))))()())))))))())))())))(())))())))))))()))))))))()((()(())))))))))(())))())))())))))))))()())))()))))))))('] | |
]; | |
/** | |
* Complete the 'getMin' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts STRING s as parameter. | |
* | |
* @param {string} s | |
* @returns number | |
*/ | |
function getMin(s) { | |
const chars = s.split('').filter(c => !!c); | |
// Count of added opening parentheses during the parsing | |
let opened = 0; | |
// Count of non-closed parentheses after the parsing | |
let openingDiff = 0; | |
for (const char of chars) { | |
// Match opening parenthesis | |
if (char == '(') { | |
++openingDiff; | |
} | |
// Match closing parenthesis | |
else if (char == ')') { | |
// We have matched a closing parenthesis that was not | |
// previousy opened, so we have to add an opening one | |
if (openingDiff === 0) { | |
++opened; | |
} | |
// Else we simply close a previously opened parenthesis | |
else { | |
--openingDiff; | |
} | |
} | |
// Unexpected character | |
else { | |
throw new Error('Unexpected character ' + char); | |
} | |
} | |
return opened + openingDiff; | |
} | |
tests.forEach(([expect, test], index) => { | |
const res = getMin(test); | |
if (res === expect) { | |
console.log(`Test ${index}: OK`) | |
} else { | |
console.log(`Test ${index} failed: expected ${expect} got ${res}`) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment