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 |
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
#include <stdio.h> | |
void swap(char* a, char* b); | |
void reverse(char* str); | |
int main(){ | |
char strArray[] = "1point3acres"; | |
printf("Before Reverse: %s\n", strArray); | |
reverse(strArray); | |
printf("After Reverse: %s\n", strArray); | |
} |
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 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 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 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 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
""" | |
============================================================================ | |
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
""" | |
============================================================================ | |
Question : 2.2 Implement an algorithm to find the kth to last element of a singly linked list. | |
Solution : Faster pointer move forward Kth node first, then move forward to the end alone with slow pointer, | |
until faster pointer reaches the end, return slower pointer | |
Time Complexity : O(N) | |
Space Complexity: O(1) | |
Gist Link : https://gist.github.com/habina/f521508cef301ac0d18d | |
============================================================================ | |
""" |
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 : Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node. | |
EXAMPLE | |
Input: the node c from the linked list a->b->c->d->e | |
Result: nothing isreturned, but the new linked list looks like a- >b- >d->e | |
Solution : Copy the following pointer content, and relink the next to the next next pointer | |
Time Complexity : O(1) | |
Space Complexity: O(1) | |
Gist Link : https://gist.github.com/habina/1effe686ec65dbadf1ec |
OlderNewer