Quick way to check if all the characters of a string are same

Given a string, check if all the characters of the string are same or not.

Examples:

Input : s = "geeks"
Output : No

Input : s = "gggg" 
Output : Yes

Simple Way


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.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find whether the string
// has all same characters or not.
#include <iostream>
using namespace std;
  
bool allCharactersSame(string s)
{
    int n = s.length();
    for (int i = 1; i < n; i++)
        if (s[i] != s[0])
            return false;
  
    return true;
}
  
// Driver code
int main()
{
    string s = "aaa";
    if (allCharactersSame(s))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find whether the String
// has all same characters or not.
import java.io.*;
  
public class GFG{
  
static boolean allCharactersSame(String s)
{
    int n = s.length();
    for (int i = 1; i < n; i++)
        if (s.charAt(i) != s.charAt(0))
            return false;
          
    return true;
}
  
// Driver code
    static public void main (String[] args){
        String s = "aaa";
    if (allCharactersSame(s))
        System.out.println("Yes");
    else
        System.out.println("No");
          
    }
}
  
// This Code is contributed by vt_m.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find whether the string 
# has all same characters or not.
  
# Function to check the string has
# all same characters or not .
def allCharactersSame(s) :
    n = len(s)
    for i in range(1, n) :
        if s[i] != s[0] :
            return False
  
    return True
  
# Driver code
if __name__ == "__main__" :
      
    s = "aaa"
    if allCharactersSame(s) :
        print("Yes")
    else :
        print("No")
  
# This code is contributed by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find whether the string
// has all same characters or not.
using System;
  
public class GFG{
  
static bool allCharactersSame(string s)
{
    int n = s.Length;
    for (int i = 1; i < n; i++)
        if (s[i] != s[0])
            return false;
  
    return true;
}
  
// Driver code
    static public void Main (String []args){
        string s = "aaa";
    if (allCharactersSame(s))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
          
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP program to find whether 
// the string has all same 
// characters or not.
function allCharactersSame($s)
{
    $n = strlen($s);
    for ($i = 1; $i < $n; $i++)
        if ($s[$i] != $s[0])
            return false;
  
    return true;
}
  
// Driver code
$s = "aaa";
if (allCharactersSame($s))
echo "Yes";
else
echo "No";
  
// This code is contributed
// by ChitraNayal
?>

chevron_right



Output:

Yes

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).

filter_none

edit
close

play_arrow

link
brightness_4
code

// A quick C++ program to find whether the
// string has all same characters or not.
#include <iostream>
using namespace std;
  
bool allCharactersSame(string s)
{
    return (s.find_first_not_of(s[0]) == string::npos);
}
  
// Driver code
int main()
{
    string s = "aaa";
    if (allCharactersSame(s))
        cout << "Yesn";
    else
        cout << "Non";
  
    return 0;
}

chevron_right


Output:

yes

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 contribute@geeksforgeeks.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.



My Personal Notes arrow_drop_up

Improved By : vt_m, AnkitRai01, Ita_c



Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.