题目:
思路:
遍历,每次取相应长度的字串并计算比值,如果为1立即退出
提交:
while(line = readline()) {
let totalLength = line.length
let subLength = Number(readline())
let subStrObj = {}
let maxGCTimes = 0
let maxGCStr = ''
for(let i = 0; i < totalLength; i ++) {
// 不完整子串不考虑
if (i + subLength <= totalLength) {
let key = line.slice(i, i + subLength)
let currentGCTimes = 0
for (let j = 0; j < subLength; j++) {
if (key[j] === 'G' || key[j] === 'C') {
currentGCTimes++
}
}
if (currentGCTimes === subLength) {
maxGCStr = key
break
}
// console.log(currentGCTimes, key)
if (currentGCTimes > maxGCTimes) {
maxGCStr = key
maxGCTimes = currentGCTimes
}
}
}
console.log(maxGCStr)
}