📌  相关文章
📜  检查给定的字符串是否是线性的

📅  最后修改于: 2021-09-06 06:15:22             🧑  作者: Mango

给定字符串str ,任务是检查给定字符串是否是线性的。如果它是线性的,则打印“是”,否则打印“否”

例子:

办法:检查是否给定的字符串的形式为“xxaxabcxabcdxabcdexab ……”那么,我们要检查,如果在索引0,1,3,6,10个字符,…是平等与否。
如果上述索引处的所有字符都相等,则给定的字符串是线性字符串,否则不是。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if the given
// string is linear or not
int is_linear(string s)
{
    int tmp = 0;
 
    char first = s[0];
 
    // Iterate over string
    for (int pos = 0; pos < s.length();
        pos += tmp) {
 
        // If character is not same as
        // the first character then
        // return false
        if (s[pos] != first) {
            return false;
        }
 
        // Increment the tmp
        tmp++;
    }
 
    return true;
}
 
// Driver Code
int main()
{
    // Given String str
    string str = "aapaxyayziabcde";
 
    // Function Call
    if (is_linear(str)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
   
class GFG{
  
// Function to check if the given
// string is linear or not
static boolean is_linear(String s)
{
    int tmp = 0;
    char first = s.charAt(0);
  
    // Iterate over string
    for(int pos = 0; pos < s.length();
            pos += tmp)
    {
         
        // If character is not same as
        // the first character then
        // return false
        if (s.charAt(pos) != first)
        {
            return false;
        }
         
        // Increment the tmp
        tmp++;
    }
    return true;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given String str
    String str = "aapaxyayziabcde";
 
    // Function call
    if (is_linear(str))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to check if the given
# is linear or not
def is_linear(s):
 
    tmp = 0
    first = s[0]
    pos = 0
     
    # Iterate over string
    while pos < len(s):
 
        # If character is not same as
        # the first character then
        # return false
        if (s[pos] != first):
            return False
         
        # Increment the tmp
        tmp += 1
        pos += tmp
         
    return True
 
# Driver Code
if __name__ == '__main__':
 
    # Given String str
    str = "aapaxyayziabcde"
 
    # Function call
    if (is_linear(str)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
  
class GFG{
     
// Function to check if the given
// string is linear or not
static bool is_linear(String s)
{
    int tmp = 0;
    char first = s[0];
 
    // Iterate over string
    for(int pos = 0; pos < s.Length;
            pos += tmp)
    {
         
        // If character is not same as
        // the first character then
        // return false
        if (s[pos] != first)
        {
            return false;
        }
 
        // Increment the tmp
        tmp++;
    }
    return true;
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given String str
    String str = "aapaxyayziabcde";
 
    // Function call
    if (is_linear(str))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by grand_master


Javascript


输出:
Yes

时间复杂度: O(log N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live