题解 | #密码截取#
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
package main import ( "fmt" ) func isPalindrome(s string) bool { // 判断字符串是否是回文串 left := 0 right := len(s) - 1 for left < right { if s[left] != s[right] { return false } left++ right-- } return true } func findLongestPalindrome(s string) int { // 找出最长的有效密码串 maxLength := 0 for i := 0; i < len(s); i++ { for j := i; j < len(s); j++ { substring := s[i : j+1] if isPalindrome(substring) && len(substring) >= maxLength { maxLength = len(substring) } } } return maxLength } func main() { var s string fmt.Scanln(&s) maxLength := findLongestPalindrome(s) fmt.Println(maxLength) }