Help me in solving TRISWP problem

My issue

include <bits/stdc++.h>
using namespace std;

int main() {
// your code goes here

int t; cin>>t;

while(t--){
    int n; cin>>n;
    string s;
    cin>>s;
    
    char p1 = s[1], p2 = s[2], p3 = s[0];
    int ans=1;
    
    for(int i=1; i<=n-3; i++){
        if(p1==s[i-1] and p2==s[i+1] and p3==s[i+2]) ;
        else ans++;
        p1 = s[i+1];
        p2 = s[i+2];
        p3 = s[i];
    }
    
    cout<<ans<<endl;
    
}

}
whatis wrog here. explain your approches too:

My code

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here

    int t; cin>>t;
    
    while(t--){
        int n; cin>>n;
        string s;
        cin>>s;
        
        char p1 = s[1], p2 = s[2], p3 = s[0];
        int ans=1;
        
        for(int i=1; i<=n-3; i++){
            if(p1==s[i-1] and p2==s[i+1] and p3==s[i+2]) ;
            else ans++;
            p1 = s[i+1];
            p2 = s[i+2];
            p3 = s[i];
        }
        
        cout<<ans<<endl;
        
    }
    
}

Problem Link: Triangular Swaps Practice Coding Problem - CodeChef

To be very honest, I don’t really understand your approach. Are you making temporal shiftings to then that be compared to the next triad?

The problem with that approach is that your conditionals aren’t actually making anything. Was that on purpose?

But the main problem is that in your else statement, you are counting several times one corner case that should be counted only once:

Say, this:

“aaabaaabaaa”

Everytime it gets to any of the 3 substrings “aaa” it gets counted once. But that is wrong because you should count the different strings, and those shifts make the same string, so that can’t be counted more than once.

You have 3 base cases to be considered:

  1. When you have the 3 characters as the same
  2. 2 out of 3 are the same
  3. All are different.

These are:

  1. If all the 3 characters are the same, then you get the original string. That can happen more than once as the example above. So let’s flag that as “original_string”.

  2. If 2 out of 3 are the same, then there’s a risk you can have the same string in other shiftings. How to know? Let’s think of this corner case:

“aaba”
You can get “abaa” 2 times

By knowing that 0, 1 and 3 are the same, but 2 is different, you know that it is going to be a repetition. Try this out in a notebook.

  1. All different characters make a unique new strings.

Taking that into consideration:

#include <bits/stdc++.h>
#define ll long long
using namespace std;

void solve(){

    ll N;
    cin >> N;
    
    string S;
    cin >> S;
    

    bool original_string = false;
    ll amount = 0;
    
    for(ll i=0; i<N-2; i++){
        
        if ((S[i] == S[i+1]) && (S[i+1] == S[i+2]))
            original_string = true;
        
        else if ((S[i] == S[i+1]) && (S[i+1] != S[i+2]))
            if (i+3 < N)
                if (S[i+3] == S[i])
                    continue;
                else
                    amount++;
            else
                amount++;
        else
            amount++;
    }
        
    cout << amount + int(original_string) << "\n";

}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int T;

	cin >> T;
	while(T--){
		solve();
	}
}

Please let me know if my code or my explanation is not clear enough.