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 revertWordsInString(str) { | |
let inputStr = str.trim(); | |
let words = [], curWord = ""; | |
for(let i=0; i<str.length; i++) { | |
// если встретили не пробел, формируем слово | |
if (str[i] !== " ") { | |
curWord += str[i]; | |
} |
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 compressString(str) { | |
// regexp | |
const zip = str.replace(/(.)\1{0,}/g, | |
group => (group[0] + (group.length > 1 ? group.length : ''))); | |
return zip; | |
} | |
const str = 'AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD'; | |
console.info("Compression of string: ", compressString(str)); |
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 CompressString(str) { | |
let acc = 0; | |
let char; | |
let result = str.split('').reduce((r, currentValue, index, arr) => { | |
if (currentValue != arr[index-1]) { | |
if (char) { | |
r += ((acc > 1) ? `${char}${acc}` : char); | |
} | |
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 checkUniqueSymbols(inputStr) { | |
if (inputStr) { | |
const hasDuplicateSymbols = inputStr.match(/(\w)(.+)?\1/) !== null; | |
return !hasDuplicateSymbols; | |
} | |
return false; | |
} | |
console.info("abcadez", checkUniqueSymbols("abcadez")); // false | |
console.info("abcdefg", checkUniqueSymbols("abcdefg")); // true | |
console.info(" try ", checkUniqueSymbols(" try ")); // true |
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 waterVolume(arr) { | |
let leftMax = 0, rightMax = 0, volume = 0; | |
let left = 0, right = 0; | |
let leftLocalMaxArr = [], rightLocalMaxArr = []; | |
for(let i=0; i<arr.length; i++) { | |
left = i; | |
right = arr.length - 1 - i; | |
// локальные максимумы слева |
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 giveMyMoney(money, coins) { | |
let needCoins = [], minCoins = []; minCoins[0] = 0; | |
// перебираем все суммы до заданной суммы (можно начать с мин.купюры) | |
for (let sum = 1; sum <= money; sum++) { | |
// по умолчанию эта сумма не для выдачи | |
minCoins[sum] = Number.MAX_VALUE; |
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 getUniqueNumberInArray(arr) { | |
if (!arr || !arr.length) { | |
return null; | |
} | |
let result = arr[0]; | |
for (let i=1; i<arr.length; i++) { | |
result ^= arr[i]; // XOR each element in array | |
} | |
return result; |
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 compressString(inputStr) { | |
if (inputStr) { | |
// частный случай: если строка состоит из 1го символа | |
// или сжатие приведет к увеличению строки | |
let length = inputStr.length, inputStrArr = [...inputStr]; | |
let compressStrLength = getCompressionStringLength(inputStrArr); | |
if (length === 1 || compressStrLength >= length) { | |
return inputStr; | |
} |
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 containsDividers(intArray, X) { | |
// HashMap: число -> кол-во вхождений в массив | |
let hashMapElements = {}; | |
for(let item of intArray) { | |
if (hashMapElements[item]) { | |
++hashMapElements[item]; | |
} else { | |
hashMapElements[item] = 1; | |
} |
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 getFirstUniqueSymbol(inputStr) { | |
if (inputStr) { | |
const length = inputStr.length; | |
// частный случай: строка из 1го символа | |
if (length === 1) { | |
return inputStr; | |
} | |
// записываем в массив кол-во каждого символа: индекс элемента = код символа в строке |
OlderNewer