给你一个字符串s
,首尾相连成一个环形,
请你在环中找出o
字符出现了偶数次最长子字符串的长度.
备注:
1 <= s.lenth <= 5x10^5
s
只包含小写英文字母
输入是一个小写字母组成的字符串
输出是一个整数
alolobo
6
最长子字符串之一是alolob
,它包含2
个o
looxdolx
7
最长子字符串oxdolxl
,由于是首尾连接一起的,
所以最后一个x
和开头的l
是连接在一起的此字符串包含2
个o
bcbcbc
6
这个示例中,字符串bcbcbc
本身就是最长的,
因为o
都出现了0
次
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Author: Amos
* E-mail: amos@amoscloud.com
* Date: 2021/12/2
* Time: 19:16
* Description:
*/
public class Main0077 {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
String line = scanner.nextLine();
solution(line);
}
}
private static void solution(String line) {
int max = 0;
for (int i = 0; i < line.length(); i++) {
String str = line.substring(i) + line.substring(0, i);
for (int j = 0; j < str.length(); j++) {
String sub = str.substring(0, i + 1);
int count = countO(sub);
if (count % 2 == 0 && sub.length() > max) {
max = sub.length();
break;
}
}
}
System.out.print(max);
}
private static int countO(String str) {
int count = 0;
for (char c : str.toCharArray()) {
if (c == 'o') count++;
}
return count;
}
}