Given a string, check if all the characters of the string are same or not.
Input : s = "geeks" Output : No Input : s = "gggg" Output : Yes
To find whether string has all the same characters. Traverse the whole string from index 1 and check whether that character matches with first character of string or not. If yes, than match until string size. If no, than break the loop.
Quick Way (Not time complexity wise, but in terms of number of lines of code)
The idea is to use find_first_not_of() in C++ STL.
find_first_not_of() finds and returns position of first character that does not match a specified character (or any of the specified characters in case of a string).
This article is contributed by Jatin Goyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Check whether second string can be formed from characters of first string
- Check if both halves of the string have same set of characters
- Check if both halves of the string have same set of characters in Python
- Check if a String contains any index with more than K active characters
- Check if the characters of a given string are in alphabetical order
- Check if characters of one string can be swapped to form other
- Program to check if first and the last characters of string are equal
- Check if a given string is made up of two alternating characters
- Check if a string is made up of K alternating characters
- Check whether the frequencies of all the characters in a string are prime or not
- Check whether count of distinct characters in a string is Prime or not
- Check if string follows order of characters defined by a pattern or not | Set 3
- Check if a string has all characters with same frequency with one variation allowed
- Check if characters of a given string can be rearranged to form a palindrome
- Python program to check if a string contains all unique characters
- Check if string follows order of characters defined by a pattern or not | Set 2
- Check if a string can be split into substrings starting with N followed by N characters
- Check if string follows order of characters defined by a pattern or not | Set 1
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Check if the characters in a string form a Palindrome in O(1) extra space