//school
public class Main
{
public static void main(String[] args)
{
String str = "CCAAAAATTT!";
char x = str.charAt(0);
char maxchar = str.charAt(0);
int cnt=1;
// one bc the letter itself counts
int max = 1;
// one bc minimum count is also 1, but 0 also works
for(int i = 1; i < str.length(); i++)
// i starts at 1 cuz x location is charAt(0) so you gotta look ahead to 1
{
if(x==str.charAt(i))
{
cnt++;
// if previously saved letter equal to this future letter, count 1 up
}
else
{
cnt=1;
// if previously saved letter != this future letter, reset count
}
if(max<cnt)
{
max = cnt;
maxchar = x;
// renew max count and max-counted-character if current count is higher
}
x = str.charAt(i);
// change letter to the future letter (keep moving on)
}
System.out.println(maxchar + " " + max);
}
}
top of page
실제 작동 상태를 확인하려면 라이브 사이트로 이동하세요.
CB Unit 4 Progress Check FRQ Q1
CB Unit 4 Progress Check FRQ Q1
댓글 0개
좋아요
댓글(0)
bottom of page