查找字符串中相同字符连续出现的最大次数
输入一串字符串,字符串长度不超过100,查找字符串中相同字符连续出现的最大次数
a = input().strip() last = ' ' res = 0 cnt = 0 for letter in a: if letter != last: if res < cnt: res = cnt last = letter cnt = 1 else: cnt += 1 if res < cnt: res = cnt print(res)
s = input() l = 0 ret = 0 for i, j in enumerate(s): if j != s[l]: ret = max(ret, i - l) l = i ret = max(ret, len(s) - l) print(ret)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); String[] split = s.split(""); Integer count=0; for (int i = 0; i < split.length; i++) { int c1=1; if (i!= split.length-1){ for (int j=i+1;j<split.length;j++){ if (split[j].equals(split[i])){ c1++; }else { break; } } } count=Math.max(c1,count); i=i+c1; } System.out.println(count); } }