题解 | #DNA序列#

DNA序列

https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a

package main

import (
	"fmt"
	"strings"
)

func main() {

	str := ""
	n := 0
	fmt.Scan(&str)
	fmt.Scan(&n)
	//记录G 、C 出现最多的 index
	maxIndex := 0
	//记录G 、C 出现最多的个数
	maxCount := 0
	//滑动窗口求解出maxIndex
	for i := 0; i < len(str)-n; i++ {
		tempStr := str[i : i+n]
		count := 0
		count += strings.Count(tempStr, "C")
		count += strings.Count(tempStr, "G")
		if count > maxCount {
			maxCount = count
			maxIndex = i
		}
	}
	//根据 maxIndex输出 子串即可
	fmt.Println(str[maxIndex : maxIndex+n])

}

全部评论

相关推荐

06-27 12:30
延安大学 C++
实习+外包,这两个公司底层融为一体了,如何评价呢?
一表renzha:之前面了一家外包的大模型,基本上都能答出来,那面试官感觉还没我懂,然后把我挂了,我都还没嫌弃他是外包,他把我挂了……
第一份工作能做外包吗?
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务