# Single flip Substring

Last Updated : 24 Apr, 2023

Given a string of Letters ‘A‘ and ‘B‘, the task is to make a special flip operation exactly once on the given string. Special Flip Operation: Choose a substring and change all ‘A’ to ‘B’ and all ‘B’ to ‘A’. After performing Special Flip Operation exactly once., determine whether it is possible to make the string palindrome.

Examples:

Input: “ABBA”
Output: Possible
Explanation: Though the string is already palindrome we have performed the operation exactly once. Flip the substring [2, 3] and the string becomes “AAAA” this is also a palindrome.

Input: “ABBAB”
Output: Possible
Explanation: Flip the substring [1, 2] and the string becomes “BABAB” which is a palindrome.

Input: “BAAABAA”
Output: Impossible
Explanation: It is impossible to make the string palindrome by exactly one operation.

Approach: This can be solved with the following idea:

It is clear that we can perform the operation exactly one time. So, though the string is already palindrome we have to do the Special Flip Operation one time on it. But it is no problem . We can choose the whole string and flip this it also is a palindrome. But when the string is not we have to check whether it’s possible or not.

Steps involved in the implementation of code:

• We have to traverse a for loop till half of the array.
• Checking the respective character of the 1st string and 2nd string.
• If these are the same then increasing the substring size is different.
• if these are different then increase the count of the special substring.
• If the vector size() is less than equal to 1 then it is possible.
• Else it is impossible.

Below is the implementation of the above approach:

## C++

```// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;

// Function to check whether it is possible
// to make string palindrome
void PallindromeString(string s)
{

int n = s.size();
int cnt = 0;
int count_of_substr = 0;
for (int i = 0; i < n / 2; i++) {

// Cheking if it is equal to
// respective charecter in 2nd half
if (s[i] == s[n - i - 1]) {

if (cnt > 0) {

// If it is same increase
// the count of special
// substring if cnt > 0
count_of_substr++;
cnt = 0;
}
}
else {
cnt++;
}
}

if (cnt > 0)
count_of_substr++;
if (count_of_substr++ <= 1) {
cout << "Possible" << endl;
return;
}
cout << "Impossible" << endl;

return;
}

// Driver code
int main()
{
string s = "ABBABAA";

// Function call
PallindromeString(s);

return 0;
}```

## Java

```import java.util.*;

public class Main {

// Function to check whether it is possible
// to make string palindrome
public static void PallindromeString(String s) {

int n = s.length();
int cnt = 0;
int count_of_substr = 0;
for (int i = 0; i < n / 2; i++) {

// Cheking if it is equal to
// respective charecter in 2nd half
if (s.charAt(i) == s.charAt(n - i - 1)) {

if (cnt > 0) {

// If it is same increase
// the count of special
// substring if cnt > 0
count_of_substr++;
cnt = 0;
}
}
else {
cnt++;
}
}

if (cnt > 0)
count_of_substr++;
if (count_of_substr++ <= 1) {
System.out.println("Possible");
return;
}
System.out.println("Impossible");
return;
}

// Driver code
public static void main(String[] args) {
String s = "ABBABAA";

// Function call
PallindromeString(s);
}
}
// This code is contributed by Prajwal Kandekar
```

## Python3

```# Python code for the above approach:

# Function to check whether it is
# possible to make string palindrome
def PallindromeString(s):
n = len(s)
cnt = 0
count_of_substr = 0
for i in range(n // 2):
# Checking if it is equal to
# respective character in 2nd half
if s[i] == s[n - i - 1]:
if cnt > 0:
# If it is same increase the count
# of special substring if cnt > 0
count_of_substr += 1
cnt = 0
else:
cnt += 1
if cnt > 0:
count_of_substr += 1
if count_of_substr <= 1:
print("Possible")
return
print("Impossible")
return

s = "ABBABAA"
# Function Call
PallindromeString(s)

# This code is contributed by lokesh.
```

## C#

```// C# code for the above approach:

using System;

public class GFG {

// Function to check whether it is possible to make
// string palindrome
static void PallindromeString(String s)
{
int n = s.Length;
int cnt = 0;
int count_of_substr = 0;
for (int i = 0; i < n / 2; i++) {

// Cheking if it is equal to
// respective charecter in 2nd half
if (s[i] == s[n - i - 1]) {

if (cnt > 0) {

// If it is same increase
// the count of special
// substring if cnt > 0
count_of_substr++;
cnt = 0;
}
}
else {
cnt++;
}
}

if (cnt > 0)
count_of_substr++;
if (count_of_substr++ <= 1) {
Console.WriteLine("Possible");
return;
}
Console.WriteLine("Impossible");
return;
}

static public void Main()
{

// Code
String s = "ABBABAA";

// Function call
PallindromeString(s);
}
}

// This code is contributed by karthik.```

## Javascript

```//Javascript code for the above approach:

// Function to check whether it is
// possible to make string palindrome
function PallindromeString(s) {
var n = s.length;
var cnt = 0;
var count_of_substr = 0;
for (var i = 0; i < n / 2; i++) {
// Checking if it is equal to
// respective character in 2nd half
if (s[i] == s[n - i - 1]) {
if (cnt > 0) {
// If it is same increase the count
// of special substring if cnt > 0
count_of_substr += 1;
cnt = 0;
}
}
else {
cnt += 1;
}
}
if (cnt > 0) {
count_of_substr += 1;
}
if (count_of_substr <= 1) {
console.log("Possible");
return;
}
console.log("Impossible");
return;
}
var s = "ABBABAA";
// Function Call
PallindromeString(s);

// This code is contributed by Tapesh(tapeshdua420)```
Output

`Possible`

Time Complexity: O(N) where N equals to half of the string size.
Auxiliary Space : O(1)