Open In App

Check if given string satisfies the following conditions

Given a string S of length L, where L is a perfect square, the task is to check if the given string satisfies the following conditions:



If the condition is satisfied, print “Yes”. Otherwise, print “No”.

Examples: 



Input: S = ”abacdaeaafaghaia”
Output: Yes
Explanation: 
 

 Input: S = ”abacdaeabfaghaia” 
Output: No 

Approach: The idea is to traverse the matrix A[][] where its corresponding character in the matrix M[][] is 1. Follow the steps below to solve the problem: 

Below is the implementation of the above approach: 




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if given string
// satisfies the given conditions
void isValid(string s)
{
 
    // Dimensions
    int n = sqrt(s.length());
 
    char check = s[0];
 
    // Left diagonal
    for (int i = 0; i < n; i++) {
        int x = i, y = i;
 
        // Right diagonal
        while (x >= 0 && y < n) {
            if (s[(n * x) + y] != check
                || s[(n * y) + x] != check) {
 
                // Conditions not satisfied
                cout << "No" << endl;
                return;
            }
            x--;
            y++;
        }
    }
 
    // Print Yes
    cout << "Yes" << endl;
}
 
// Driver Code
int main()
{
 
    // Given String
    string str = "abacdaeaafaghaia";
 
    // Function call
    isValid(str);
 
    return 0;
}




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




# Python3 program for the above approach
import math
 
# Function to check if given string
# satisfies the given conditions
def isValid(s):
     
    # Dimensions
    n = int(math.sqrt(len(s)))
    check = s[0]
 
    # Left diagonal
    for i in range(n):
        x = i
        y = i
         
        # Right diagonal
        while (x >= 0 and y < n):
            if (s[n * x + y] != check or
                s[n * x + x] != check):
                     
                # Conditions not satisfied
                print("No")
                return
                 
            x -= 1
            y += 1
             
    # Print Yes
    print("Yes")
     
# Driver Code
 
# Given String
str = "abacdaeaafaghaia"
 
# Function call
isValid(str)
 
# This code is contributed by avanitrachhadiya2155




// C# program for the above approach 
using System;
  
class GFG{
  
// Function to check if given string
// satisfies the given conditions
static void isValid(string s)
{
     
    // Dimensions
    int n = (int)Math.Sqrt(s.Length);
     
    char check = s[0];
     
    // Left diagonal
    for(int i = 0; i < n; i++)
    {
        int x = i, y = i;
         
        // Right diagonal
        while (x >= 0 && y < n)
        {
            if (s[(n * x) + y] != check ||
                s[(n * y) + x] != check)
            {
                 
                // Conditions not satisfied
                Console.Write("No");
                return;
            }
            x--;
            y++;
        }
    }
  
    // Print Yes
    Console.Write("Yes");
}
 
// Driver code
public static void Main()
{
     
    // Given String
    string str = "abacdaeaafaghaia";
  
    // Function call
    isValid(str);
}
}
 
// This code is contributed by sanjoy_62




<script>
 
// Javascript program to implement
// the above approach
 
// Function to check if given string
// satisfies the given conditions
function isValid(s)
{
      
    // Dimensions
    let n = Math.sqrt(s.length);
       
    let check = s[0];
       
    // Left diagonal
    for(let i = 0; i < n; i++)
    {
        let x = i, y = i;
          
        // Right diagonal
        while (x >= 0 && y < n)
        {
            if (s[(n * x) + y]!= check ||
                s[(n * y) + x] != check)
            {
                  
                // Conditions not satisfied
                document.write("No");
                return;
            }
            x--;
            y++;
        }
    }
      
    // Print Yes
    document.write("Yes");
}
   
    // Driver Code
     
    // Given String
    let str = "abacdaeaafaghaia";
      
    // Function call
    isValid(str);
 
// This code is contributed by souravghosh0416.
</script>

Output: 
Yes

 

Time Complexity: O(L1/2) where L is the length of the given string.
Auxiliary Space: O(1) since constant space has been used


Article Tags :