You can not just take the file and send it. When Polycarp trying to send a file in the social network "Codehorses", he encountered an unexpected problem. If the name of the file contains three or more "x" (lowercase Latin letters "x") in a row, the system considers that the file content does not correspond to the social network topic. In this case, the file is not sent and an error message is displayed.
Determine the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. Print 0 if the file name does not initially contain a forbidden substring "xxx".
You can delete characters in arbitrary positions (not necessarily consecutive). If you delete a character, then the length of a string is reduced by 11. For example, if you delete the character in the position 22 from the string "exxxii", then the resulting string is "exxii".
The first line contains integer nn (3≤n≤100)(3≤n≤100) — the length of the file name.
The second line contains a string of length nn consisting of lowercase Latin letters only — the file name.
Print the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. If initially the file name dost not contain a forbidden substring "xxx", print 0.
6 xxxiii
1
5 xxoxx
0
10 xxxxxxxxxx
8
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String s = scan.next();
char[] a = s.toCharArray();
int num = 0;
int t = 0;
for(int i = 0; i < n; i++){
if(a[i]=='x'){
num++;
}if(i!=0){
if(a[i]!='x'&&a[i-1]=='x'){
if(num>=3){
t = t + (num - 2);
}
num = 0;
}
}
}
if(a[n-1]=='x'&&num>=3){
System.out.println(t+(num-2));
}else{
System.out.println(t);
}
}
}