Test if given string satisfies the next situations

by admin

Given a string S of size L, the place L is a perfect square, the duty is to test if the given string satisfies the next situations:

  • Insert the characters of the string right into a sq. matrix A[][] of dimensions √L x √L in row-wise method.
  • Initialize one other matrix M[][] with 0s. Fill the left diagonal with 1. Now, for 1 current within the left diagonal, fill its correesponding proper diagonal with 1s.
  • Now, test if all of the indices in matrix M[][] which accommodates 1, accommodates the identical character in A[][].

If the situation is happy, print “Sure”. In any other case, print “No”.

Examples:

Enter: S = ”abacdaeaafaghaia”
Output: Sure
Clarification:

Enter: S = ”abacdaeabfaghaia” 
Output: No 

Strategy: The concept is to traverse the matrix A[][] the place its corresponding character within the matrix M[][] is 1. Observe the steps beneath to unravel the issue:

  • Calculate the size of the matrix as N = √L.
  • Iterate over the left diagonal by visiting every cell, A[i][i] the place 1<= i<= N.
  • For each component of the left diagonal at cell A[i][i], initialize variables x and y with i and traverse its corresponding proper diagonal by visiting the character S[x*N + y] and S[y*N + x] and decrement x every time by 1 and increment y every time by 1 to maneuver alongside the following cells in the suitable diagonals, whereas x is just not smaller than 0 and y is smaller than N.
  • If all characters are discovered to be similar within the above step, print “Sure”. In any other case, print “No” if any mismatch is discovered.

Under is the implementation of the above method:

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

void isValid(string s)

{

  

    

    int n = sqrt(s.size());

  

    char test = s[0];

  

    

    for (int i = 0; i < n; i++) {

        int x = i, y = i;

  

        

        whereas (x >= 0 && y < n) {

            if (s[(n * x) + y] != test

                || s[(n * y) + x] != test) {

  

                

                cout << "No" << endl;

                return;

            }

            x--;

            y++;

        }

    }

  

    

    cout << "Sure" << endl;

}

  

int primary()

{

  

    

    string str = "abacdaeaafaghaia";

  

    

    isValid(str);

  

    return 0;

}

Time Complexity: O(L) the place L is the size of the given string.
Auxiliary House: O(L)

Consideration reader! Don’t cease studying now. Pay money for all of the essential DSA ideas with the DSA Self Paced Course at a student-friendly value and develop into business prepared.



If you happen to like GeeksforGeeks and wish to contribute, you can too write an article utilizing contribute.geeksforgeeks.org or mail your article to [email protected] See your article showing on the GeeksforGeeks primary web page and assist different Geeks.

Please Enhance this text should you discover something incorrect by clicking on the “Enhance Article” button beneath.




Related Posts

Leave a Comment