Skip to content
Related Articles

Related Articles

Check if string is right to left diagonal or not
  • Last Updated : 20 Aug, 2020

Given string str of perfect square length. The task is to check whether the given string is a right to left diagonal or not. If it is a right to left diagonal then print “Yes” else print “No”.

Let the string be “abcdefghi”. It can be broken as: 
“abc” 
“def” 
“ghi”
if the character c, e, and g are equal then the given string is a right to left diagonal otherwise not. 

It means, first break the string into a square box and check if right to left diagonal’s all character are the same or not. If it is the same then print “Yes”, otherwise print “No”. 

Examples: 

Input: str = “abcxabxcaxbcxabc” 
Output: Yes 
Explanation: Break the string in square box, see below 
abcx 
abxc 
axbc 
xabc 
So, right ot left diagonal have same character. 



Input: str=”abcdxabcxdabxcdaxbcdaxbcd” 
Output: No 
Explanation: Break the string in square box, see below 
abcdx 
abcxd 
abxcd 
axbcd 
axbcd 
So, right to left diagonal haven’t same character.

Approach: Follow the steps given below to solve the problem

  • Calculate the length of the string.
  • Check whether the length perfect square of any number or not.
  • If not perfect square then, Print No
  • Else proceed below steps
    • Let length is the perfect square of k
    • Check the indexes k – 1, 2k – 1, 3k – 1…and so on.
    • If character at all the indexes is same then
      • Print YES
    • Else
      • Print NO

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to Check if the
// given string is right to
// left diagonal or not
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if the given
// string is right to left diagonal or not
int is_rtol(string s)
{
    int tmp = sqrt(s.length()) - 1;
  
    char first = s[tmp];
  
    // Iterate over string
    for (int pos = tmp;
         pos < s.length() - 1; pos += tmp) {
  
        // If character is not same as
        // the first character then
        // return false
        if (s[pos] != first) {
            return false;
        }
    }
  
    return true;
}
  
// Driver Code
int main()
{
    // Given String str
    string str = "abcxabxcaxbcxabc";
  
    // Function Call
    if (is_rtol(str)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if the 
// given string is right to 
// left diagonal or not 
import java.io.*;
  
class GFG{
      
// Function to check if the given
// string is right to left diagonal or not
public static boolean is_rtol(String s)
{
    int tmp = (int)(Math.sqrt(s.length())) - 1;
    char first = s.charAt(tmp);
      
    // Iterate over string
    for(int pos = tmp; pos < s.length() - 1
            pos += tmp)
    {
          
        // If character is not same as
        // the first character then
        // return false
        if (s.charAt(pos) != first)
        {
            return false;
        }
    }
    return true;
}
  
// Driver Code
public static void main(String args[])
{
      
    // Given String str
    String str = "abcxabxcaxbcxabc";
      
    // Function call
    if (is_rtol(str))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
  
// This code is contributed by grand_master

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Check if the
# given is right to
# left diagonal or not
from math import sqrt, floor, ceil
  
# Function to check if the given
# is right to left diagonal or not
def is_rtol(s):
  
    tmp = floor(sqrt(len(s))) - 1
  
    first = s[tmp]
  
    # Iterate over string
    for pos in range(tmp, len(s) - 1, tmp):
  
        # If character is not same as
        # the first character then
        # return false
        if (s[pos] != first):
            return False
  
    return True
  
# Driver Code
if __name__ == '__main__':
      
    # Given String str
    str = "abcxabxcaxbcxabc"
  
    # Function Call
    if (is_rtol(str)):
        print("Yes")
    else:
        print("No")
          
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if the 
// given string is right to 
// left diagonal or not 
using System;
  
class GFG{
      
// Function to check if the given
// string is right to left diagonal or not
public static bool is_rtol(String s)
{
    int tmp = (int)(Math.Sqrt(s.Length)) - 1;
    char first = s[tmp];
      
    // Iterate over string
    for(int pos = tmp; pos < s.Length - 1; 
            pos += tmp)
    {
          
        // If character is not same as
        // the first character then
        // return false
        if (s[pos] != first)
        {
            return false;
        }
    }
    return true;
}
  
// Driver Code
public static void Main(String []args)
{
      
    // Given String str
    String str = "abcxabxcaxbcxabc";
      
    // Function call
    if (is_rtol(str))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
  
// This code is contributed by amal kumar choubey 

chevron_right


Output: 

Yes

Time complexity: O(N)

Auxiliary space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :