Check if string is right to left diagonal or not
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.
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
- Else
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>
Time complexity: O(N)
Auxiliary space: O(1)
Similar Reads
Check if the given string is linear or not Given string str, the task is to check whether the given string is linear or not. If it is linear then print "Yes" else print "No".  Let the string be "abcdefghij". It can be broken as: "a" "bc" "def" "ghij" if the character a, b, c, and are equal then the given string is linear otherwise not. Ther
4 min read
Queries to check if substring[L...R] is palindrome or not Given a string str and Q queries. Every query consists of two numbers L and R. The task is to print if the sub-string[L...R] is palindrome or not. Examples: Input: str = "abacccde", Q[][] = {{0, 2}, {1, 2}, {2, 4}, {3, 5}} Output: Yes No No Yes Input: str = "abaaab", Q[][] = {{0, 1}, {1, 5}} Output:
12 min read
Check if left and right shift of any string results into given string Given a non-empty string S consisting of only lowercase English letters. The task is to find if there exists any string which has left shift and right shift both equal to string S. If there exists any string then print Yes, otherwise print No. Examples: Input: S = "abcd" Output: No Explanation: Ther
7 min read
Check if a string follows a^nb^n pattern or not Given string str, return true string follows pattern anbn, i.e., it has a's followed by b's such that the number of a's and b's are same. Examples: Input : str = "aabb" Output : Yes Input : str = "abab" Output : No Input : str = "aabbb" Output : No The idea is to first count a's. If number of a's is
8 min read
Check if a given string is a Reverse Bitonic String or not Given a string str, the task is to check if that string is a Reverse Bitonic string or not. If the string str is reverse Bitonic string, then print "YES". Otherwise, print "NO". A Reverse Bitonic String is a string in which the characters are arranged in decreasing order followed by increasing order
6 min read