+ 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')

16th Sep 2022, 10:58 AM
Venkatesh
Venkatesh - avatar
6 Answers
+ 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)))
16th Sep 2022, 3:15 PM
Lothar
Lothar - avatar
+ 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.
16th Sep 2022, 3:21 PM
Lothar
Lothar - avatar
+ 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
16th Sep 2022, 11:18 AM
Jayakrishna 🇼🇳
+ 2
Thank you it's working
17th Sep 2022, 12:59 PM
Venkatesh
Venkatesh - avatar
+ 1
I solved it but with a diifferent way a = input().lower() print ( str( sorted(a) == sorted(set(a)) ).lower() )
16th Sep 2022, 1:22 PM
SoloProg
SoloProg - avatar
+ 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() )
17th Sep 2022, 12:34 PM
SoloProg
SoloProg - avatar