Last active
April 24, 2021 09:06
-
-
Save asankulov/796cdd871509911a060044dcffe07bc8 to your computer and use it in GitHub Desktop.
asankulov-kylych_Backend20120Hour20Test20Pre-screen
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 main() { | |
const origArr = [ | |
'AMOR', 'XISELA', 'JAMON', 'ROMA', 'OMAR', 'MORA', 'ESPONJA', | |
'RAMO', 'JAPONES', 'ARMO', 'MOJAN', 'MARO', 'ORAM', 'MONJA', 'ALEXIS' | |
]; | |
groupSimilarWordsAndPrintThem(origArr); | |
const str = 'foo(foo(bar(str))(opg))blim'; | |
removeParenthesesAndReverseTheWordsInsideThem(str); | |
}()); | |
function groupSimilarWordsAndPrintThem(arr) { | |
const hashMap = {}; | |
for (let i = 0; i < arr.length; i++) { | |
let tempStr = arr[i].split('').sort().join(''); | |
if (!hashMap[tempStr]) hashMap[tempStr] = arr[i]; | |
else hashMap[tempStr] += ` - ${arr[i]}`; | |
} | |
for (let key in hashMap) { | |
console.log(hashMap[key]); | |
} | |
} | |
function removeParenthesesAndReverseTheWordsInsideThem(str) { | |
let res = '', stk1 = [], stk2 = []; | |
for (let i = 0; i < str.length; i++) { | |
if (str[i] === '(') { | |
stk1.push(str[i]); | |
} else if (str[i] === ')') { | |
stk1.pop(); | |
if (stk1.length) { | |
if (stk2.length) { | |
stk2.push(stk2.pop().split('').reverse().join('')); | |
} | |
} | |
else { | |
while (stk2.length) { | |
res += stk2.pop(); | |
} | |
} | |
} else if (stk1.length) { | |
let tempStr = ''; | |
while (str[i] !== ')' && str[i] !== '(') { | |
tempStr = str[i] + tempStr; | |
i++; | |
} | |
i--; | |
stk2.push(tempStr); | |
} else { | |
res += str[i]; | |
} | |
} | |
console.log(res); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment