+ 2
Any mistake in below code?
Some test cases passed... a=input() b=0 for i in range(0,len(a)): for j in range(1,len(a)): if a[i]==a[j]: b=b+1 if b>0: print('false') elif b==0: print('true')
6 Réponses
+ 3
SoloProg ,
sorting is not required when using a set. we can just compare the length of the string with the length of the set:
a = input().lower()
print(len(a) == len(set(a)))
+ 3
Arun Venkatesh.N ,
the issue of your code is that the second for loop with the range() always starts generating the same start value. see the modification in the code:
a=input()
b=0
for i in range(0, len(a)):
for j in range(i + 1, len(a)): # <<<
if a[i] == a[j]:
b=b+1
if b > 0:
print('false')
elif b == 0:
print('true')
a further impovement could be to use break when variable `b` is incremented. be aware that we have to exit both loops.
this procedre reduces the number of iterations.
+ 2
i = 1, j = 1
a[1] == a[1] true , lead to wrong.
Instead of 1 in range(), try i+1
or add condition i!=j must
+ 2
Thank you it's working
+ 1
I solved it but with a diifferent way
a = input().lower()
print ( str( sorted(a) == sorted(set(a)) ).lower() )
+ 1
Lothar ,
This is a solution using LEN to make the code work.
s = input()
b = len(s) == len(set(s))
print ( str(b).lower() )