function solution(m, musicinfos) {
var answer = '';
const getMinutes = hhmm => {
const [hh, mm] = hhmm.split(':')
return +hh * 60 + +mm;
}
const firstPitch = m[0]
const isMatched = (fullStream, stream) => {
const findedIndex = fullStream.indexOf(stream)
if(findedIndex!==-1){
if(fullStream[findedIndex + stream.length] === '#'){
return isMatched(fullStream.slice(findedIndex + stream.length +1) , stream)
}else{
return true;
}
}
return false;
}
const matched = []
musicinfos.map(musicinfo => {
const [start, end, name, stream] = musicinfo.split(',')
const duration = getMinutes(end) - getMinutes(start)
const streamLength = stream.split('').filter(c => c !=='#').length;
const repeatTimes = Math.floor(duration/streamLength);
let remainTimes = duration % streamLength;
let fullStream = stream.repeat(repeatTimes)
let i = 0
while(remainTimes){
const pitch = stream[i]
const nextPitch = stream[i+1]
fullStream += pitch
i++
if(nextPitch==='#'){
fullStream += '#'
i++
}
remainTimes--
}
return [duration, name, fullStream]
}).sort((a,b) => a[0]>b[0] ? 1 : -1).forEach((musicinfo => {
const [duration , name, fullStream] = musicinfo;
if(isMatched(fullStream, m)){
matched.push([name, duration]);
}
}))
return matched.length ? matched.pop()[0] : "(None)";
}