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

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: Yes



Input: 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:

  1. All the characters at odd indices must be same.
  2. All the characters at even indices must be same.
  3. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 characters
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
int main()
{
    string str = "ABAB";
  
    if (isTwoAlter(str))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.io.*;
  
class GFG
{
  
// Function that returns true if the string
// is made up of two alternating characters
static 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 code
public 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..

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Function that returns true if the string
# is made up of two alternating characters
def 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 code
if __name__ == "__main__":
    str = "ABAB"
  
    if (isTwoAlter(str)):
        print ( "Yes")
    else:
        print ("No")
  
# This code is contributed by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using 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

chevron_right


Output:

Yes

Time Complexity : O(N)
Auxiliary Space : O(1)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Recommended Posts:

    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.