Check if given string satisfies the following conditions
Last Updated :
06 Oct, 2022
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:
- Insert the characters of the string into a square matrix A[][] of dimensions ?L x ?L in row-wise manner.
- Initialize another matrix M[][] with 0s. Fill the left diagonal with 1. Now, for 1 present in the left diagonal, fill its corresponding right diagonal with 1s.
- Now, check if all the indices in matrix M[][] which contains 1, contains the same character in A[][].
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:
- Calculate the dimensions of the matrix as N = ?L.
- Iterate over the left diagonal by visiting each cell, A[i][i] where 1<= i<= N.
- For every element of the left diagonal at cell A[i][i], initialize variables x and y with i and traverse its corresponding right diagonal by visiting the character S[x*N + y] and S[y*N + x] and decrement x each time by 1 and increment y each time by 1 to move along the next cells in the right diagonals, while x is not smaller than 0 and y is smaller than N.
- If all characters are found to be same in the above step, print “Yes”. Otherwise, print “No” if any mismatch is found.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void isValid(string s)
{
int n = sqrt (s.length());
char check = s[0];
for ( int i = 0; i < n; i++) {
int x = i, y = i;
while (x >= 0 && y < n) {
if (s[(n * x) + y] != check
|| s[(n * y) + x] != check) {
cout << "No" << endl;
return ;
}
x--;
y++;
}
}
cout << "Yes" << endl;
}
int main()
{
string str = "abacdaeaafaghaia" ;
isValid(str);
return 0;
}
|
Java
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);
}
}
|
Python3
import math
def isValid(s):
n = int (math.sqrt( len (s)))
check = s[ 0 ]
for i in range (n):
x = i
y = i
while (x > = 0 and y < n):
if (s[n * x + y] ! = check or
s[n * x + x] ! = check):
print ( "No" )
return
x - = 1
y + = 1
print ( "Yes" )
str = "abacdaeaafaghaia"
isValid( str )
|
C#
using System;
class GFG{
static void isValid( string s)
{
int n = ( int )Math.Sqrt(s.Length);
char check = s[0];
for ( int i = 0; i < n; i++)
{
int x = i, y = i;
while (x >= 0 && y < n)
{
if (s[(n * x) + y] != check ||
s[(n * y) + x] != check)
{
Console.Write( "No" );
return ;
}
x--;
y++;
}
}
Console.Write( "Yes" );
}
public static void Main()
{
string str = "abacdaeaafaghaia" ;
isValid(str);
}
}
|
Javascript
<script>
function isValid(s)
{
let n = Math.sqrt(s.length);
let check = s[0];
for (let i = 0; i < n; i++)
{
let x = i, y = i;
while (x >= 0 && y < n)
{
if (s[(n * x) + y]!= check ||
s[(n * y) + x] != check)
{
document.write( "No" );
return ;
}
x--;
y++;
}
}
document.write( "Yes" );
}
let str = "abacdaeaafaghaia" ;
isValid(str);
</script>
|
Time Complexity: O(L1/2) where L is the length of the given string.
Auxiliary Space: O(1) since constant space has been used
Share your thoughts in the comments
Please Login to comment...