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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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 a string is made up of K alternating characters
- Program to check if first and the last characters of string are equal
- Check if characters of one string can be swapped to form other
- 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 whether count of distinct characters in a string is Prime or not
- Check if characters of a given string can be rearranged to form a palindrome
- 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 string follows order of characters defined by a pattern or not | Set 2
- Python program to check if a string contains all unique characters