-
-
Save hoanbka/7cc71707643bc6ab77b1ec6aba82df02 to your computer and use it in GitHub Desktop.
sherlock-and-valid-string
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
/*https://www.hackerrank.com/challenges/sherlock-and-valid-string?h_r=next-challenge&h_v=zen*/ | |
function isValid(str){ | |
// Complete this function | |
var map = new Map(); | |
for(var i=0; i < str.length;i++){ | |
if(map.has(str.charAt(i))){ | |
map.set(str.charAt(i),map.get(str.charAt(i))+1); | |
}else{ | |
map.set(str.charAt(i),1); | |
} | |
} | |
var arrKey = Array.from(map.keys()); | |
var count =0; | |
var difference = 0; | |
var check = 0; | |
for(var i=0;i< arrKey.length;i++){ | |
if(map.get(arrKey[i])==1) check ++; | |
if(map.get(arrKey[i])!==map.get(arrKey[0])){ | |
count ++; | |
difference = map.get(arrKey[i])- map.get(arrKey[0]); | |
} | |
if(count >1){ | |
if(check ==1) continue; | |
return 'NO'; | |
} | |
} | |
if(count ==1 ){ | |
if(check ==1 || difference ==1 || difference == -1){ | |
return 'YES' | |
} | |
return 'NO'; | |
} | |
return 'YES'; | |
} | |
var str = 'aabbcc'; | |
var str2 = 'aabbccc'; // xóa 1 phần tử c thì a,b,c có 3 lần xuất hiên => YES | |
var str3 = 'ibfdgaeadiaefgbhbdghhhbgdfgeiccbiehhfcggchgghadhdhagfbahhddgghbdehidbibaeaagaeeigffcebfbaieggabcfbiiedcabfihchdfabifahcbhagccbdfifhghcadfiadeeaheeddddiecaicbgigccageicehfdhdgafaddhffadigfhhcaedcedecafeacbdacgfgfeeibgaiffdehigebhhehiaahfidibccdcdagifgaihacihadecgifihbebffebdfbchbgigeccahgihbcbcaggebaaafgfedbfgagfediddghdgbgehhhifhgcedechahidcbchebheihaadbbbiaiccededchdagfhccfdefigfibifabeiaccghcegfbcghaefifbachebaacbhbfgfddeceababbacgffbagidebeadfihaefefegbghgddbbgddeehgfbhafbccidebgehifafgbghafacgfdccgifdcbbbidfifhdaibgigebigaedeaaiadegfefbhacgddhchgcbgcaeaieiegiffchbgbebgbehbbfcebciiagacaiechdigbgbghefcahgbhfibhedaeeiffebdiabcifgccdefabccdghehfibfiifdaicfedagahhdcbhbicdgibgcedieihcichadgchgbdcdagaihebbabhibcihicadgadfcihdheefbhffiageddhgahaidfdhhdbgciiaciegchiiebfbcbhaeagccfhbfhaddagnfieihghfbaggiffbbfbecgaiiidccdceadbbdfgigibgcgchafccdchgifdeieicbaididhfcfdedbhaadedfageigfdehgcdaecaebebebfcieaecfagfdieaefdiedbcadchabhebgehiidfcgahcdhcdhgchhiiheffiifeegcfdgbdeffhgeghdfhbfbifgidcafbfcd' | |
// console.log(isValid('aabbccc')) | |
console.log(isValid(str3)); | |
console.log(isValid(str2)); | |
console.log(isValid('aaabbbcccd')) // YES vì xóa d thì a,b,c đều có 3 lần xuất hiện | |
console.log(isValid('abccddee')) // NO | |
console.log(isValid('abbbcccddd')); |
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
/*https://www.hackerrank.com/challenges/sherlock-and-valid-string?h_r=next-challenge&h_v=zen*/ | |
function isValid(str){ | |
// Complete this function | |
var map = new Map(); | |
for(var i=0; i < str.length;i++){ | |
if(map.has(str.charAt(i))){ | |
map.set(str.charAt(i),map.get(str.charAt(i))+1); | |
}else{ | |
map.set(str.charAt(i),1); | |
} | |
} | |
var arrKey = Array.from(map.keys()); | |
var count =0; | |
var difference = 0; | |
var check = 0; | |
for(var i=0;i< arrKey.length;i++){ | |
if(map.get(arrKey[i])==1) check ++; | |
if(map.get(arrKey[i])!==map.get(arrKey[0])){ | |
count ++; | |
difference = map.get(arrKey[i])- map.get(arrKey[0]); | |
} | |
if(count >1){ | |
if(check ==1) continue; | |
return 'NO'; | |
} | |
} | |
if(count ==1 ){ | |
if(check ==1 || difference ==1 || difference == -1){ | |
return 'YES' | |
} | |
return 'NO'; | |
} | |
return 'YES'; | |
} | |
var str = 'aabbcc'; | |
var str2 = 'aabbccc'; // xóa 1 phần tử c thì a,b,c có 3 lần xuất hiên => YES | |
var str3 = 'ibfdgaeadiaefgbhbdghhhbgdfgeiccbiehhfcggchgghadhdhagfbahhddgghbdehidbibaeaagaeeigffcebfbaieggabcfbiiedcabfihchdfabifahcbhagccbdfifhghcadfiadeeaheeddddiecaicbgigccageicehfdhdgafaddhffadigfhhcaedcedecafeacbdacgfgfeeibgaiffdehigebhhehiaahfidibccdcdagifgaihacihadecgifihbebffebdfbchbgigeccahgihbcbcaggebaaafgfedbfgagfediddghdgbgehhhifhgcedechahidcbchebheihaadbbbiaiccededchdagfhccfdefigfibifabeiaccghcegfbcghaefifbachebaacbhbfgfddeceababbacgffbagidebeadfihaefefegbghgddbbgddeehgfbhafbccidebgehifafgbghafacgfdccgifdcbbbidfifhdaibgigebigaedeaaiadegfefbhacgddhchgcbgcaeaieiegiffchbgbebgbehbbfcebciiagacaiechdigbgbghefcahgbhfibhedaeeiffebdiabcifgccdefabccdghehfibfiifdaicfedagahhdcbhbicdgibgcedieihcichadgchgbdcdagaihebbabhibcihicadgadfcihdheefbhffiageddhgahaidfdhhdbgciiaciegchiiebfbcbhaeagccfhbfhaddagnfieihghfbaggiffbbfbecgaiiidccdceadbbdfgigibgcgchafccdchgifdeieicbaididhfcfdedbhaadedfageigfdehgcdaecaebebebfcieaecfagfdieaefdiedbcadchabhebgehiidfcgahcdhcdhgchhiiheffiifeegcfdgbdeffhgeghdfhbfbifgidcafbfcd' | |
// console.log(isValid('aabbccc')) | |
console.log(isValid(str3)); | |
console.log(isValid(str2)); | |
console.log(isValid('aaabbbcccd')) // YES vì xóa d thì a,b,c đều có 3 lần xuất hiện | |
console.log(isValid('abccddee')) // NO | |
console.log(isValid('abbbcccddd')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment