文字列内の特定の文字の出現回数をカウントして、連続する出現を1と見なす方法を解明しようとしています。文字列内の文字の出現をカウントする(1つとして連続する)
public static void main (String Args[]){
Scanner in =new Scanner(System.in);
String s =in.next();
//HashSet hd =new HashSet();
int flag=0;
int count=1;
char a=s.charAt(0);
for(int i=1;i<s.length();i++)
{
//hd.add(s.charAt(i));
if(a==s.charAt(i))
{
continue;
}
else
{
a=s.charAt(i);
count++;
}
}
System.out.println(count);
}
お試しはどこですか?あなたのコード –
[文字列中の文字の出現を数える簡単な方法]の可能な複製(https://stackoverflow.com/questions/6100712/simple-way-to-count-character-occurrences-in-a-string) – vinS
@ LalitVerma私は、ユニークな出現を1つとしてカウントし、連続する出現も1つカウントし、最後にそれらを追加したい。 –