Check if a given string is made up of two alternating characters | Java implementation of the approach ; Function that returns true if the string is made up of two alternating characters ; Check if ith character matches with the character at index ( i + 2 ) ; If string consists of a single character repeating itself ; Driver code
import java . io . * ; class GFG { static boolean isTwoAlter ( String s ) { for ( int i = 0 ; i < s . length ( ) - 2 ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( i + 2 ) ) { return false ; } } if ( s . charAt ( 0 ) == s . charAt ( 1 ) ) return false ; return true ; } public static void main ( String [ ] args ) { String str = " ABAB " ; if ( isTwoAlter ( str ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }