# Check if the given string is linear or not

Given a 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.
Therefore if the string is of the form “xxaxabcxabcdxabcdexab…” then it is called as the linear string

Examples:

Input: str = “aapaxyayziabcde”
Output: Yes
Explanation:

ap
axy
ayzi
abcde
All the broken string have same character as the first character.
Input: str = “bbobfycd”
Output: No
Explanation:

bo
bfy
cd
Here b=b=b!=c

Approach: To check if the given string is of the form “xxaxabcxabcdxabcdexab…” then we have to check if characters at index 0, 1, 3, 6, 10, … are equals or not.
If all the character at the above indexes are equal then the given string is Linear String otherwise it is not.
Below is the implementation of the above approach:

## 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; ` ` `  `    ``// 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; ` ` `  `    ``// 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 `

Output:

```Yes
```

Time Complexity: O(log N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Code and Let Code

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.