VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-the-given-string-is-linear-or-not/

⇱ Check if the given string is linear or not - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if the given string is linear or not

Last Updated : 11 May, 2021

Given 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 characters 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: 


Output: 
Yes

 

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

Comment