Last active
October 12, 2019 13:45
-
-
Save shahidcodes/272bc874deec30589239c4b361c7b422 to your computer and use it in GitHub Desktop.
Find Minimum Occurance Of A String In String With No Repeated Letters
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
const find = (str, char)=>{ | |
let indexes = [] | |
for(let i=0; i<str.length; i++){ | |
// console.log(str.charAt(0) + "==" + char) | |
if(str.charAt(i) == char){ | |
indexes.push(i); | |
} | |
} | |
return indexes; | |
} | |
function search(target){ | |
const searchLiterals = "BALLOON".split(""); | |
let indexMaps = {} | |
for(let lt of searchLiterals){ | |
let indexes = find(target, lt); | |
console.log(lt + ", " +indexes) | |
indexMaps[lt] = indexes.length | |
} | |
let minNumber = Math.min.apply(null, Object.values(indexMaps)); | |
console.log(minNumber) | |
return minNumber; | |
} | |
search("KABBEALLOOAASSNN") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment