how to find a string has duplicate elements or not
Respostas da entrevista
Sigiloso
9 de mar. de 2017
def func(x):
y = {}
for i in x:
if i in y:
y[i]+=1
else:
y[i]=1
for i,j in y.items():
if j > 1:
return "Duplicates Present"
return "No Duplicates"
print (func("Raviteja"))
Sigiloso
13 de mar. de 2017
Approach taken by Raviteja works optimally in O(n) time, but takes additional O(n) space over this approach which uses bit manipulation. Algorithm used below uses O(n) time and O(1) space.
def is_unique_characters(input_string):
# Checks corner case
if len(input_string)>256:
return False
val = 0
checker = 0
for ch in input_string:
val = ord(ch)-ord('a')
# When element repeats
if (checker & (1 0:
return False
checker |= (1<
Sigiloso
13 de mar. de 2017
**Corrected**
Approach taken by Raviteja works optimally in O(n) time, but takes additional O(n) space over this approach which uses bit manipulation. Algorithm used below uses O(n) time and O(1) space works for characters and integers.
def is_unique_characters(input_string):
# Checks corner case
if len(input_string)>256:
return False
val = 0
checker = 0
for ch in input_string:
val = ord(ch)-ord('a')
# When element repeats
if (checker & (1 0):
return False
checker |= (1<