Check if all digits are the same python

Convert the string to a set and see if it has more than 1 element in it:

return (len(set(s)) == 1)

The entire function could be:

def rejectSerial(s: str) -> bool:
    return (
        not s.isdecimal()
        or len(set(s)) == 1
    )

This assumes that s is a string, as implied by the int() call in your original post. If s is already an int then you don't need to check whether it's a number and the function would simply be:

def rejectSerial(s: int) -> bool:
    return len(set(str(s))) == 1

If the type of s is completely unknown and you want the function to accept either numbers OR string representations of numbers and simply return True for other input types, then you could just convert it to a string and then apply the string version of this check:

def rejectSerial(s) -> bool:
    return (
        not str(s).isdecimal()
        or len(set(str(s))) == 1
    )

This will return True if you pass random types like None or a list to it, since the string representation of those will fail the isdecimal() check.

If you want the function be be able to accept arbitrary (non-int) values without raising an exception, but only return False for int values (not string representations of int values), and you want to enforce the 9-digit positive number requirement, THEN you might want:

def rejectSerial(s) -> bool:
    return not (
        isinstance(s, int)
        and 100000000 <= s <= 999999999
        and len(set(str(s))) != 1
    )

Given a positive integer N, the task is to check whether all the digits of the given integer N are the same or not. If found to be true, then print Yes. Otherwise, print No.

Examples:

Input: N = 222
Output: Yes

Input: N = 232
Output: No

Naive Approach: The simplest approach to solve the given problem is to iterate over all the digits of the given number N and if there exists any distinct digit then print Yes. Otherwise, print No.

Below is the implementation of the above approach:

C++

#include

using namespace std;

string checkSameDigits(int N)

{

    int digit = N % 10;

    while (N != 0)

    {

        int current_digit = N % 10;

        N = N / 10;

        if (current_digit != digit)

        {

            return "No";

        }

    }

    return "Yes";

}

int main()

{

    int N = 222;

    cout << (checkSameDigits(N));

    return 0;

}

Java

import java.io.*;

class GFG {

    public static String checkSameDigits(int N)

    {

        int digit = N % 10;

        while (N != 0) {

            int current_digit = N % 10;

            N = N / 10;

            if (current_digit != digit) {

                return "No";

            }

        }

        return "Yes";

    }

    public static void main(String args[])

        throws IOException

    {

        int N = 222;

        System.out.println(

            checkSameDigits(N));

    }

}

Python3

def checkSameDigits(N) :

    digit = N % 10;

    while (N != 0) :

        current_digit = N % 10;

        N = N // 10;

        if (current_digit != digit) :

            return "No";

    return "Yes";

if __name__ == "__main__" :

    N = 222;

    print(checkSameDigits(N));

C#

using System;

class GFG {

    static string checkSameDigits(int N)

    {

        int digit = N % 10;

        while (N != 0) {

            int current_digit = N % 10;

            N = N / 10;

            if (current_digit != digit) {

                return "No";

            }

        }

        return "Yes";

    }

    public static void Main()

    {

        int N = 222;

        Console.Write(checkSameDigits(N));

    }

}

Javascript

    }

    return "Yes";

}

    var N = 222;

    document.write(checkSameDigits(N));

Time Complexity: O(log10N)
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized by forming another number, say M of the same length of the given number N with the rightmost digit of N assuming N has all same digits and then comparing it with N. Now, M is of type (K*111….), where K is any digit from N.

Now to create the number M consisting of the only 1s, the sum of a Geometric Progression can be used as illustrated for the count of digits as 3:

Consider the first term(say a) as 1 and the common ratio(say r) as 10. Now for the value count of digits(say D) as 3 the sum of Geometric Progression is given by:

=> Sum = 

Check if all digits are the same python

=> Sum =  

Check if all digits are the same python

=> Sum = 

Check if all digits are the same python

 -> Sum = 111

From the above observations, generate the number M and check if K*M is the same as the N or not. If found to be true, then print Yes. Otherwise, print No.

Below is the implementation of the above approach:

C++

#include

using namespace std;

string checkSameDigits(int N)

{

    int length = int(log10(N)) + 1;

    int M = (int(pow(10, length)) - 1) / (10 - 1);

    M *= N % 10;

    if (M == N)

        return "Yes";

    return "No";

}

int main()

{

    int N = 222;

    cout << checkSameDigits(N);

}

Java

import java.io.*;

class GFG {

    public static String checkSameDigits(int N)

    {

        int length = ((int)Math.log10(N)) + 1;

        int M = ((int)Math.pow(10, length) - 1)

                / (10 - 1);

        M *= N % 10;

        if (M == N)

            return "Yes";

        return "No";

    }

    public static void main(String args[])

        throws IOException

    {

        int N = 222;

        System.out.println(

            checkSameDigits(N));

    }

}

Python3

import math

def checkSameDigits(N) :

    length = int(math.log10(N)) + 1;

    M = (int(math.pow(10, length)) - 1)// (10 - 1);

    M *= N % 10;

    if (M == N) :

        return "Yes";

    return "No";

if __name__ == "__main__" :

    N = 222;

    print(checkSameDigits(N));

C#

using System;

class GFG {

    public static String checkSameDigits(int N)

    {

        int length = ((int)Math.Log10(N)) + 1;

        int M = ((int)Math.Pow(10, length) - 1) / (10 - 1);

        M *= N % 10;

        if (M == N)

            return "Yes";

        return "No";

    }

    public static void Main()

    {

        int N = 222;

        Console.WriteLine(checkSameDigits(N));

    }

}

Javascript

 Time Complexity: O(1)
Auxiliary Space: O(1)


How do you check if all digits of a number are same Python?

Practical Data Science using Python.
number := convert num as string..
freq := a map containing frequencies of digits of number..
freq_values := make a new set by taking all digit frequency values from number..
if size of freq_values is same as 1, then. return True..
return False..

How do you check if a number has all same digits?

Given two integers A and B, the task is to check whether both the numbers have an equal number of digits. Approach: While both the numbers are > 0, keep dividing both the numbers by 10. Finally, check if both the numbers are 0. If any one of them is not 0 then they had an unequal numbers of digits.

How do you check if numbers are the same in Python?

Python String isnumeric() Method The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False. Exponents, like ² and ¾ are also considered to be numeric values. "-1" and "1.5" are NOT considered numeric values, because all the characters in the string must be numeric, and the - and the .

How do you compare two digits in Python?

Both “is” and “==” are used for object comparison in Python. The operator “==” compares values of two objects, while “is” checks if two objects are same (In other words two references to same object).