function solution(str) {
const words = str.split(" ")[0];
const word = str
.split(" ")[0]
.split("")
.sort((a, b) => a.localeCompare(b))[parseInt(str.split(" ")[1]) - 1];
return words.indexOf(word);
}
console.log(solution("hAkDAjByBq 4"));
/*
hAkDAjByBq 4
=>
6
*/