This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
============================================================================ | |
Question :Write code to remove duplicates from an unsorted linked list. | |
FOLLOW UP | |
How would you solve this problem if a temporary buffer is not allowed? | |
Solution : iterate through the whole, add char to a set | |
if char already existed in set, skip this node | |
Time Complexity : Average Case: O(1), Worst Case: O(N) | |
Space Complexity: O(N) | |
Gist Link : https://gist.github.com/habina/3896531754d629b2032f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getIndex(x, y, width): | |
return x + y * width | |
def printMatrix(matrix, m, n): | |
""" | |
Print Matrix row by row | |
""" | |
for i in range(m*n): | |
print(matrix[i], end="\t") | |
if((i + 1) % n == 0): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getIndex(x, y, width): | |
""" | |
Return the index in 1D array | |
""" | |
index = x + y * width | |
return index | |
def printMatrix(matrix, width): | |
""" | |
Print Matrix row by row |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def stringCompression(originalString): | |
"""Compress string by using the counts of repeated characters | |
Input: aabcccccaaa | |
Output: a2b1c5a3 | |
If the length of string bigger than the compressed string | |
Then return the original string | |
""" | |
if(originalString == ""): | |
return originalString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def replaceSpace(string): | |
"""Replace all spaces in a string with '%20'""" | |
"Split string by space, save into a list" | |
newList = string.split() | |
"str.join concatenate list at the end" | |
newString = "%20".join(newList) | |
return newString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def permutationString(firstStr, secondStr): | |
"Return True if the second string is a permutation of the other" | |
"""If the length of two strings are different, | |
directly return false | |
""" | |
if(len(firstStr) != len(secondStr)): | |
return False | |
"Change all characters to lowercase" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def uniqueString(str): | |
"Return True if the string has all unique characters." | |
allChar = [] | |
for i in range(256): | |
allChar.append(True) | |
for char in str: | |
if(allChar[ord(char)]): | |
allChar[ord(char)] = False | |
else: | |
return False |
NewerOlder