Check if string is right to left diagonal or not
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++
#include <bits/stdc++.h>
using namespace std;
int is_rtol(string s)
{
int tmp = sqrt (s.length()) - 1;
char first = s[tmp];
for ( int pos = tmp;
pos < s.length() - 1; pos += tmp) {
if (s[pos] != first) {
return false ;
}
}
return true ;
}
int main()
{
string str = "abcxabxcaxbcxabc" ;
if (is_rtol(str)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.io.*;
class GFG{
public static boolean is_rtol(String s)
{
int tmp = ( int )(Math.sqrt(s.length())) - 1 ;
char first = s.charAt(tmp);
for ( int pos = tmp; pos < s.length() - 1 ;
pos += tmp)
{
if (s.charAt(pos) != first)
{
return false ;
}
}
return true ;
}
public static void main(String args[])
{
String str = "abcxabxcaxbcxabc" ;
if (is_rtol(str))
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
from math import sqrt, floor, ceil
def is_rtol(s):
tmp = floor(sqrt( len (s))) - 1
first = s[tmp]
for pos in range (tmp, len (s) - 1 , tmp):
if (s[pos] ! = first):
return False
return True
if __name__ = = '__main__' :
str = "abcxabxcaxbcxabc"
if (is_rtol( str )):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
public static bool is_rtol(String s)
{
int tmp = ( int )(Math.Sqrt(s.Length)) - 1;
char first = s[tmp];
for ( int pos = tmp; pos < s.Length - 1;
pos += tmp)
{
if (s[pos] != first)
{
return false ;
}
}
return true ;
}
public static void Main(String []args)
{
String str = "abcxabxcaxbcxabc" ;
if (is_rtol(str))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function is_rtol(s)
{
let tmp = (Math.sqrt(s.length)) - 1;
let first = s[tmp];
for (let pos = tmp; pos < s.length - 1; pos += tmp)
{
if (s[pos] != first)
{
return false ;
}
}
return true ;
}
let str = "abcxabxcaxbcxabc" ;
if (is_rtol(str))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time complexity: O(N)
Auxiliary space: O(1)
Last Updated :
15 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...