Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Check if string is right to left diagonal or not

  • Difficulty Level : Basic
  • Last Updated : 15 Jun, 2021

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. 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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++




// 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;
}

Java




// 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

Python3




# 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

C#




// 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

Javascript




<script>
    // Javascript program to check if the
    // given string is right to
    // left diagonal or not
     
    // Function to check if the given
    // string is right to left diagonal or not
    function is_rtol(s)
    {
        let tmp = (Math.sqrt(s.length)) - 1;
        let first = s[tmp];
 
        // Iterate over string
        for(let 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;
    }
     
    // Given String str
    let str = "abcxabxcaxbcxabc";
       
    // Function call
    if (is_rtol(str))
    {
        document.write("Yes");
    }
    else
    {
        document.write("No");
    }
 
// This code is contributed by divyeshrabadiya07.
</script>
Output: 
Yes

 

Time complexity: O(N)

Auxiliary space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :