rravir
BAN USER
Comments (2)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
Python implementation
def longestSub (inp):
maxlen=0
st=0
ret=""
chmap=dict()
for i in xrange(len(inp)):
if (inp[i] not in chmap.keys()):
#first time seeing this character
chmap[inp[i]]=1
else:
chmap[inp[i]]+=1
#we need to advance the start of
#substring until we have unique
#characters between st and i+1
while (chmap[inp[i]] != 1):
chmap[inp[st]]-=1
st+=1
#we have one substring with unique chars
#check if this is longest so far
if (maxlen < i-st+1):
maxlen = i-st+1
ret=inp[st:i+1]
return (ret)
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
@Dmytri
- rravir November 27, 2016i don't think this is the right solution - see these cases imo is incorrect:
cowbell pts/1 try>./a.out {[}]
String is balanced
cowbell pts/1 try>./a.out {{]]
String is balanced