Check if given string satisfies the following conditions | Java program for the above approach ; Function to check if given string satisfies the given conditions ; Dimensions ; Left diagonal ; Right diagonal ; Conditions not satisfied ; Print Yes ; Driver Code ; Given String ; Function call
import java . util . * ; class GFG { static void isValid ( String s ) { int n = ( int ) Math . sqrt ( s . length ( ) ) ; char check = s . charAt ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = i , y = i ; while ( x >= 0 && y < n ) { if ( s . charAt ( ( n * x ) + y ) != check || s . charAt ( ( n * y ) + x ) != check ) { System . out . print ( " No " ) ; return ; } x -- ; y ++ ; } } System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { String str = " abacdaeaafaghaia " ; isValid ( str ) ; } }