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 email@example.com. 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.
- 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 a given string is made up of two alternating characters
- Check if both halves of the string have same set of characters in Python
- 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 whether the frequencies of all the characters in a string are prime or not
- Check if the characters of a given string are in alphabetical order
- Check if characters of a given string can be rearranged to form a palindrome
- Check if a string has all characters with same frequency with one variation allowed
- 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 string follows order of characters defined by a pattern or not | Set 1
- Check if string follows order of characters defined by a pattern or not | Set 2
- Python program to check if a string contains all unique characters