Python character match in a string

Question:

Write a function repfree(s) that takes as input a string s and checks whether any character appears more than once. The function should return True if there are no repetitions and False otherwise.

I have tried this but I don’t feel this is an efficient way of solving it. Can you suggest an efficient code for this, thanks?

def repfree(s):
    newlist = []
    for i in range(0, len(s)):
        newlist.append(s[i])
    newlist2 = set(newlist)
    if len(newlist) == len(newlist2):
        print("True")
   else:
        print("False")
Asked By: Praveen KUMAR

||

Answers:

One easy way to meet this requirement is to use regular expressions. You may not be allowed to use them, but if you can, then consider this:

def repfree(s):
    if re.search(r'^.*(.).*1.*$', s):
        print("True")
    else:
        print("False")
Answered By: Tim Biegeleisen

You could do this way:

Method 1:

def repfree(s):
  if len(set(s)) == len(s):
    return True
  return False

Method 2:

def repfree(s):

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

Why set?

set will return the list of all unique characters in the string in sorted order

Example :

set('shubham')

Output:

{'a', 'b', 'h', 'm', 's', 'u'}

So,if a character in a string appears more than once,it will not be equal to the length of string itself.

Answered By: Shubham Shaswat

You could split the string characters to a set and compare the length

def repfree(s):
    se = set(string)
    print(len(se) == len(s))
Answered By: Guy

You can make your approach more efficient by removing the for loop.

len(s) == len(set(s))

Otherwise, try using any

any(s.count(c) > 1 for c in s)
Answered By: Sayse

Try

chars = 'abcdefghijklmnopqrstuvwxyz'
def repfree(s):
    for char in chars:
        count = s.count(char)
        if count > 1:
            return False
    return True

But, this is a long way as it scans the list 26 times. A much better and Pythonic way would be

import collections
def repfree(s):
    results = collections.Counter(s)
    for i in results:
        if results[i] > 1:
            return False
    return True

Here, results is a dictionary that contains all the characters of s as key, and their respective frequency as value. Further, it is checked if any value is greater than 1, repetition has occurred.

Answered By: Swati Srivastava

Believe it or not, this problem can be solved in O(1) time, because every sufficiently large string contains at least one duplicate character. There are only a finite number of different Unicode characters, after all, so a string cannot be arbitrarily long while also using each Unicode character at most once.

For example, if you happen to know that your strings are formed of only lowercase letters, you can do this:

def has_repeated_char(s):
    return len(s) > 26 or len(s) != len(set(s))

Otherwise you can replace the 26 with whatever number of characters your string could possibly contain; e.g. 62 for upper- and lowercase letters and digits.

As of February 2020, the whole of Unicode has 137,994 distinct characters (Wikipedia), so if your string length is 150,000 then you can return True without searching.

Answered By: kaya3
def repfree(str):
l=list(str)
for i in range(len(l)):
    check=str.count(l[i])
    if check>1:
        flag=1
    else:
        flag=0
if(flag==1):
    return False
else:
    return True
Answered By: Shovan Karna

def matched(s):
stack = []
for char in s:
if char == ‘(‘:
stack.append(char)
elif char == ‘)’:
if not stack:
return False
stack.pop()
return not stack

Answered By: Vipul Singh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.