File Name
Hazrat Ali
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
Solition
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
string s;
cin >> s;
int ans = 0;
while (true)
{
int pos = s.find("xxx");
if (pos == string::npos)
{
break;
}
s.replace(pos, 3, "xx");
ans++;
}
cout << ans << endl;
return 0;
}