Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

How to check if the given string has these same characters or their probability is that same, which gives me the True?

For example, if there is string = "aaaa"  my result is True and:

string = "aabb" True

string = "aabbcc" True

p = "1122" True

p = "aaaaBBBB9999$$$$" True

but:

string = "korara" False

p = "33211" False

For "aaa" I can use (len (set ("aaa")) == 1), 

1 Answer

0 votes
by (36.8k points)

You can try the below code:

from collections import Counter

def check(v):

    return len(set(Counter(v).values())) <= 1

assert check("aabb")

assert check("aabbcc")

assert check("1122")

assert check("aaaaBBBB9999$$$$")

assert check("")

assert not check("korara")

assert not check("33211")

 Want to be a master in Data Science? Enroll in this Data Scientist

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...