📜  检查给定的字符串满足以下条件

📅  最后修改于: 2021-04-21 21:59:23             🧑  作者: Mango

给定长度为L的字符串S ,其中L是一个完美的平方,任务是检查给定的字符串满足以下条件:

  • 以行方式将字符串的字符插入尺寸为√Lx√L的方阵A [] []中。
  • 0 s初始化另一个矩阵M [] [] 。用1填充左对角线。现在,1只存在于左边对角线,填补其相应的含有1秒右对角线。

  • 现在,检查是否在矩阵中的所有索引包含1 M [] [],包含A [] []相同的字符。

如果满足条件,则打印“是”。否则,打印“否”

例子:

方法:想法是遍历矩阵A [] [] ,其中矩阵M [] []中的对应字符为1 。请按照以下步骤解决问题:

  • 计算矩阵的尺寸为N =√L
  • 通过访问每个像元A [i] [i]遍历左对角线,其中1 <= i <= N。
  • 对于单元格A [i] [i]上左对角线的每个元素,用i初始化变量xy并通过访问字符S [x * N + y]S [y * N + x来遍历其对应的右对角线],然后将x每次递减1 ,将y每次递减1,以沿着右对角线上的下一个单元格移动,而x不小于0y小于N。
  • 如果在上述步骤中发现所有字符都相同,则打印“是” 。否则,如果发现不匹配,则打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if given string
// satisfies the given conditions
void isValid(string s)
{
 
    // Dimensions
    int n = sqrt(s.length());
 
    char check = s[0];
 
    // Left diagonal
    for (int i = 0; i < n; i++) {
        int x = i, y = i;
 
        // Right diagonal
        while (x >= 0 && y < n) {
            if (s[(n * x) + y] != check
                || s[(n * y) + x] != check) {
 
                // Conditions not satisfied
                cout << "No" << endl;
                return;
            }
            x--;
            y++;
        }
    }
 
    // Print Yes
    cout << "Yes" << endl;
}
 
// Driver Code
int main()
{
 
    // Given String
    string str = "abacdaeaafaghaia";
 
    // Function call
    isValid(str);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
   
class GFG{
   
// Function to check if given string
// satisfies the given conditions
static void isValid(String s)
{
     
    // Dimensions
    int n = (int)Math.sqrt(s.length());
      
    char check = s.charAt(0);
      
    // Left diagonal
    for(int i = 0; i < n; i++)
    {
        int x = i, y = i;
         
        // Right diagonal
        while (x >= 0 && y < n)
        {
            if (s.charAt((n * x) + y) != check ||
                s.charAt((n * y) + x) != check)
            {
                 
                // Conditions not satisfied
                System.out.print("No");
                return;
            }
            x--;
            y++;
        }
    }
     
    // Print Yes
    System.out.print("Yes");
}
   
// Driver Code
public static void main(String[] args)
{
     
    // Given String
    String str = "abacdaeaafaghaia";
     
    // Function call
    isValid(str);
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program for the above approach
import math
 
# Function to check if given string
# satisfies the given conditions
def isValid(s):
     
    # Dimensions
    n = int(math.sqrt(len(s)))
    check = s[0]
 
    # Left diagonal
    for i in range(n):
        x = i
        y = i
         
        # Right diagonal
        while (x >= 0 and y < n):
            if (s[n * x + y] != check or
                s[n * x + x] != check):
                     
                # Conditions not satisfied
                print("No")
                return
                 
            x -= 1
            y += 1
             
    # Print Yes
    print("Yes")
     
# Driver Code
 
# Given String
str = "abacdaeaafaghaia"
 
# Function call
isValid(str)
 
# This code is contributed by avanitrachhadiya2155


C#
// C# program for the above approach 
using System;
  
class GFG{
  
// Function to check if given string
// satisfies the given conditions
static void isValid(string s)
{
     
    // Dimensions
    int n = (int)Math.Sqrt(s.Length);
     
    char check = s[0];
     
    // Left diagonal
    for(int i = 0; i < n; i++)
    {
        int x = i, y = i;
         
        // Right diagonal
        while (x >= 0 && y < n)
        {
            if (s[(n * x) + y] != check ||
                s[(n * y) + x] != check)
            {
                 
                // Conditions not satisfied
                Console.Write("No");
                return;
            }
            x--;
            y++;
        }
    }
  
    // Print Yes
    Console.Write("Yes");
}
 
// Driver code
public static void Main()
{
     
    // Given String
    string str = "abacdaeaafaghaia";
  
    // Function call
    isValid(str);
}
}
 
// This code is contributed by sanjoy_62


输出:
Yes

时间复杂度: O(L)其中L是给定字符串的长度。
辅助空间: O(L)