Archive for July 2017
Problem statement Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., might become ). You are given a target value to search. If found in the array return its index, otherwise return . You may assume no duplicate exists in the array. . . . Read more
Problem statement The string PAYPALISHIRING is written in a zigzag pattern on a given number of rows like this:

P A H N A P L S I I G Y I R 
And then read line by line: PAHNAPLSIIGYIR. Write the code that will take a string and make this conversion given a number of rows: Sample input
Sample output
. . . Read more
Problem statement Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be inplace, do not allocate extra memory. Here are some examples. . . . Read more
Problem statement Merge k sorted linked lists and return it as one sorted list. Solution We reduce the problem to the merging of two linked lists problem. This can be done in time and space. So we first write out the function to merge two sorted lists:

def merge(self, lst1, lst2): if lst1 == None: return lst2 if lst2 == None: return lst1 if lst1.val < lst2.val: lst1.next = self.merge(lst1.next, lst2) return lst1 else: lst2.next = self.merge(lst1, lst2.next) return lst2 
We can . . . Read more
Problem statement Given a set of candidate numbers (without duplicates) and a target number , find all unique combinations in where the candidate numbers sums to . The same repeated number may be chosen from unlimited number of times. Note: All numbers (including target) will be positive integers. The solution . . . Read more
Problem statement Given a linked list:

class ListNode(object): def __init__(self, x): self.val = x self.next = None 
remove the node from the end of list and return its head. Sample input
Sample output
Note: will always be valid. Solution For this we will use a two pointer technique. That is, we will iterate over our list using two . . . Read more
Problem statement Given an array of integers, are there elements in such that ? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. For example, given array , A solution set is: Solution The idea behind the . . . Read more
Problem statement Given pairs of parentheses, write a function to generate all combinations of wellformed parentheses. For example, given , a solution set is:

[ "((()))", "(()())", "(())()", "()(())", "()()()" ] 
Solution
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

class Solution(object): def generate(self, n, open_count, closed_count, s, result, visited): visited.add(s) if open_count == n and closed_count == n: result.append(s) for i in range(0, 2*nlen(s)): if open_count < n and not s+'(' in visited: self.generate(n,open_count+1, closed_count, s+'(', result, visited) if closed_count < open_count and not s+')' in visited: self.generate(n,open_count, closed_count+1, s+')', result, visited) def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ ans = [] self.generate(n, 0, 0, '', ans, set()) return ans def main(): solution = Solution() print(solution.generateParenthesis(10)) if __name__ == "__main__": main() 
Problem statement Given a string , find the longest palindromic substring in . You may assume that the maximum length of is . Sample input 1
Output 1
Sample input 1
Output 1
Solution We are going to iterate over each character in the input string . . . Read more
Problem statement Given an array of strings, group anagrams together. Sample input

["eat", "tea", "tan", "ate", "nat", "bat"] 
Sample output

[ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ] 
Note: All inputs will be in lowercase. Full code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

class Solution(object): def groupAnagrams(self, strs): """ :type strs: List[str] :rtype: List[List[str]] """ map = {} for w in strs: key = ''.join(sorted(w)) if not key in map: map[key] = [] map[key].append(w) return map.values() def main(): solution = Solution() print(solution.groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])) if __name__ == "__main__": main() 