Questions
-
Is Unique: Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?
-
Check Permutation: Given two strings, write a method to decide if one is a permutation of the other.
- URLify: Write a method to replace all spaces in a string with ‘%20’: You may assume that the string has sufficient space at the end to hold the additional characters, and that your are given the “true” length of the string. (Noe: if implementing in Java, please use a character array so that you can perform this operation in place.)
EXAMPLE:
Input: “Mr John Smith “,13
Output: “Mr%20John%20Smith” - Palindrome permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to jest dictionary words.
EXAMPLE:
Input: Tact Coa
Output: True (permutations: “taco cat”, “atco cta”, etc.) - One Away: There are three types of edits that can be performed on strings: Insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.
EXAMPLE:
pale, ple -> true pales, pale -> true pale, bale -> true pale, bake -> false - String Compression: Implement a method to perform basic string compression using the counts of repeated characters. For example the string aabcccccaaa would become a2b1c5a3. if the “compressed”string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a-z).
- Rotate matrix: Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
- Zero Matrix: Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.
- String Rotation: Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring(e.g., “waterbottle” is a rotation of “erbottlewat”)
Solution
- Is Unique
def isUnique(str): chars=set() for letter in str: if letter in chars: return False chars.add(letter) return True
without additional data structure
def isUnique(str): for i in range(len(str)-1): if str[i] in str[i+1]: return False return True
- Check Permutation:
def checkPermutation(str1,str2): dct = dict() for letter in str1: dct[letter] = dct.get(letter,0) + 1 for letter in str2: dct[letter] = dct.get(letter,0) - 1 return all([dct.values()])
- URLify
def URLify(str): lst = list(str) right = len(str)-1 left = right while left == " ": left -= 1 while left < right: if left != " ": lst[right] = lst[left] right -= 1 left -=1 else: lst[right] = "0" lst[right-1] = "2" lst[right-2] = "%" right -=3 left -=1 return "".join(lst)
- is Palindrome Permutation
def isPalindromePermutation(str): count = dict() for letter in str: if letter == " ": continue count[letter] = count.get(letter,0) + 1 flag = false for val in count.values(): if val%2 == 0: continue else: if flag: return False flag = True return True
- One Away:
def isOneAway(str1,str2): i = len(str1) - 1 j = len(str2) - 1 flag = False while i >= 0 and j >= 0: if str1[i] == str2[j]: i -= 1 j -= 1 continue else: if flag: return False flag = True if str1[i-1] == str2[j]: i -= 2 j -= 1 elif str1[i] == str2[j-1]: i -=1 j -=2 else: i -=1 j -=1 return True
- Stirng Compression:
def compressString(str): ret = "" for i in range(len(str)): count = 1 if i+1 < len(str) and str[i] == str[i+1]: count +=1 continue ret += str[i] + str(count) if len(ret) == len(str): return str else: return ret
- Rotate Matrix:
def rotateMatrix(mat): N = len(mat) for x in range(0, int(N/2)): for y in range(x, N-x-1): temp = mat[x][y] mat[x][y] = mat[y][N-1-x] mat[y][N-1-x] = mat[N-1-x][N-1-y] mat[N-1-x][N-1-y] = mat[N-1-y][x] mat[N-1-y][x] = temp
- Zero Matrix:
def isZeroMatrix(matrix): m = len(matrix) n = len(matrix[0]) for i in range(m): for j in range(n): if matrix[i][j] == 0: matrix = [[0]*n for _ in range(m)] return matrix return matrix
- String Rotation:
def isSubstring(a,b): #check if b is substring of a if b in a: return True else: return False def isRotationString(a,b): if len(a) == len(b) != 0: return isSubstring( b+b, a ) else: return False