Check if a given string is made up of two alternating characters - GeeksforGeeks

06 May, 2021
Given a string str, the task is to check whether the given string is made up of only two alternating characters.Examples:  
Input: str = “ABABABAB” Output: YesInput: str = “XYZ” Output: No  
 
Approach: In order for the string to be made up of only two alternating characters, it must satisfy the following conditions:  
All the characters at odd indices must be same.All the characters at even indices must be same.str[0] != str[1] (This is because string of type “AAAAA” where a single character is repeated a number of time will also satisfy the above two conditions)
All the characters at odd indices must be same.
All the characters at even indices must be same.
str[0] != str[1] (This is because string of type “AAAAA” where a single character is repeated a number of time will also satisfy the above two conditions)
Below is the implementation of the above approach:  
C++
Java
Python 3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function that returns true if the string// is made up of two alternating charactersbool isTwoAlter(string s){     // Check if ith character matches    // with the character at index (i + 2)    for (int i = 0; i < s.length() - 2; i++) {        if (s[i] != s[i + 2]) {            return false;        }    }     // If string consists of a single    // character repeating itself    if (s[0] == s[1])        return false;     return true;} // Driver codeint main(){    string str = "ABAB";     if (isTwoAlter(str))        cout << "Yes";    else        cout << "No";     return 0;}
// Java implementation of the approachimport java.io.*; class GFG{ // Function that returns true if the string// is made up of two alternating charactersstatic boolean isTwoAlter(String s){     // Check if ith character matches    // with the character at index (i + 2)    for (int i = 0; i < s.length() - 2; i++)    {        if (s.charAt(i) != s.charAt(i + 2))        {            return false;        }    }     // If string consists of a single    // character repeating itself    if (s.charAt(0) == s.charAt(1))        return false;     return true;} // Driver codepublic static void main (String[] args){        String str = "ABAB";     if (isTwoAlter(str))        System.out.print( "Yes");    else        System.out.print("No");}} // This code is contributed by anuj_67..
# Function that returns true if the string# is made up of two alternating charactersdef isTwoAlter( s):     # Check if ith character matches    # with the character at index (i + 2)    for i in range ( len( s) - 2) :        if (s[i] != s[i + 2]) :            return False                   #If string consists of a single    #character repeating itself    if (s[0] == s[1]):        return False     return True # Driver codeif __name__ == "__main__":    str = "ABAB"     if (isTwoAlter(str)):        print ( "Yes")    else:        print ("No") # This code is contributed by ChitraNayal
// C# implementation of the approachusing System; class GFG{     // Function that returns true if the string    // is made up of two alternating characters    static bool isTwoAlter(string s)    {             // Check if ith character matches        // with the character at index (i + 2)        for (int i = 0; i < s.Length - 2; i++)        {            if (s[i] != s[i +2])            {                return false;            }        }             // If string consists of a single        // character repeating itself        if (s[0] == s[1])            return false;             return true;    }         // Driver code    public static void Main()    {            string str = "ABAB";             if (isTwoAlter(str))            Console.WriteLine( "Yes");        else            Console.WriteLine("No");    }} // This code is contributed by AnkitRai01
<script> // Javascript implementation of the approach         // Function that returns true if the string    // is made up of two alternating characters    function isTwoAlter(s)    {        // Check if ith character matches    // with the character at index (i + 2)    for (let i = 0; i < s.length - 2; i++)    {        if (s[i] != s[i+2])        {            return false;        }    }       // If string consists of a single    // character repeating itself    if (s[0] == s[1])        return false;       return true;    }         // Driver code    let str = "ABAB";    if (isTwoAlter(str))        document.write( "Yes");    else        document.write("No");         // This code is contributed by rag2127     </script>
Yes
 
Time Complexity : O(N) Auxiliary Space : O(1) 
vt_m
ankthon
ukasp
muskan_garg
rag2127
Constructive Algorithms
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Different methods to reverse a string in C/C++
Convert string to char array in C++
Longest Palindromic Substring | Set 1
Array of Strings in C++ (5 Different Ways to Create)
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Reverse words in a given string