给定完全平方长度的字符串str 。任务是检查给定的字符串是否是从右到左的对角线。如果是从右到左对角线,则打印“是”,否则打印“否” 。
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”.
例子:
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.
方法:按照下面给出的步骤解决问题
- 计算字符串的长度。
- 检查任何数字的长度是否为完全平方数。
- 如果不是完美的正方形,则打印否
- 否则继续以下步骤
- 让长度是k的完美平方
- 检查索引k – 1、2k – 1、3k – 1 …等等。
- 如果所有索引的字符都相同,则
- 打印是
- 别的
- 打印编号
下面是上述方法的实现:
C++
// C++ program to Check if the
// given string is right to
// left diagonal or not
#include
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
Yes
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live