Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if whole string contains same character

I want to check if a string contains a character repeated zero or more times, for example:

  • If my string is aaaaaa, bbbb, c or ***** it must return true.

  • If it contains aaab, cd, or **%*** it must return false.

In other words, if the string has 2 or more unique characters, it must return false.

How to go about this in PHP?

PS: Is there a way to do it without RegEx?

like image 837
Ibrahim Azhar Armar Avatar asked Mar 24 '17 03:03

Ibrahim Azhar Armar


People also ask

How do you check if a string has all of same character Python?

The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .

How can you test whether two strings have the same values?

The equals() method compare two strings on the basis of their values or content for equality. This method compare the strings ignoring the case( case-insensitive ). It returns true if values of both the strings is same ignoring the case, else return false.

How do you check if a letter is repeated in a string C?

Start with the first character, loop through the rest of the string to see if it matches. if there is no match, then repeat for the next character.

How do I check if a string contains special characters?

To check if a string contains special characters, call the test () method on a regular expression that matches any special character. The test method will return true if the string contains at least 1 special character and false otherwise. Copied! If you consider space to be a special character, add it between the square brackets []. Copied!

How to find whether string has all the same characters in Python?

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.

Does a string contain all characters of another string?

A string consists of multiple characters and these characters can be letters, numbers, or special characters. For a beginner, it can be a bit tricky to find if it contains all characters of another string. There are numerous ways to check if a string contains all characters of another string.

What should you look for when buying a string?

Given a string, check if all the characters of the string are the same or not. 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.


3 Answers

You could split on every character then count the array for unique values.

if(count(array_count_values(str_split('abaaaa'))) == 1) {
    echo 'True';
} else {
    echo 'false';
}

Demo: https://eval.in/760293

like image 187
chris85 Avatar answered Sep 19 '22 13:09

chris85


count(array_unique(explode('', string)) == 1) ? true : false;
like image 26
LF00 Avatar answered Sep 19 '22 13:09

LF00


You can use a regular expression with a back-reference:

if (preg_match('/^(.)\1*$/', $string)) {
    echo "Same characters";
}

Or a simple loop:

$same = true;
$firstchar = $string[0];
for ($i = 1; $i < strlen($string); $i++) {
    if ($string[$i] != $firstchar) {
        $same = false;
        break;
    }
}
like image 29
Barmar Avatar answered Sep 21 '22 13:09

Barmar