-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterview.py
66 lines (52 loc) · 1.47 KB
/
Interview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'''Implement an algorithm to determine if a string has all unique characters. '''
def isUnique(string):
dict = {}
repeats = True
for char in string:
if char in dict:
repeats = False
else:
dict[char] = True
return repeats
print(isUnique('helo'))
print(isUnique('hello'))
'''What if you cannot use additional data structures?'''
def isUniqueTwo(string):
repeats = True
i = 0
j = 1
while i < len(string):
while j< len(string):
if(string[i] == string[j]):
repeats = False
j+= 1
i+= 1
j = i + 1
return repeats
print(isUniqueTwo('hello'))
print(isUniqueTwo('helo'))
''' Given two strings, write a method to decide if one is a permutation of the other '''
def permutation(first, second):
if len(first) !== len(second):
return False
dict = {}
palindrome = True
for char in first:
if dict.has_key(char):
dict[char]+=1
else:
dict[char]= 1
for char in second:
if dict.has_key(char):
dict[char] -= 1
if dict[char] <= -1:
palindrome = False
else:
palindrome = False
return palindrome
print(permutation('hello', 'hello'))
print(permutation('oh', 'ho'))
print(permutation('hllo', 'hello'))
'''write a method to replace all the spaces in a string with %20 '''
def urlify(string):
return string.replace(' ', '%20')