Posts

Plus One

  Given a   non-empty   array of digits representing a non-negative integer, increment one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself.   Example 1: Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Example 2: Input: digits = [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. Example 3: Input: digits = [0] Output: [1]   Constraints: 1 <= digits.length <= 100 0 <= digits[i] <= 9 Solution: Algorithm: 1) Add 1 to the nth element in the array. If the sum is greater than 9 then take the ten's element (save it to 'carry' ) and move to the (n-1)th element.  2) Continue step 1 till we don't have a carry. Time Complexity:  O(n) Space Complexity: O(1)

Climbing Stairs

You are climbing a stair case. It takes  n  steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Example 1: Input: 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps Example 2: Input: 3 Output: 3 Explanation: There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step   Constraints: 1 <= n <= 45 Solution: The point to observe here is that while climbing stairs, you can take either 1 or 2 steps. So for every step that you climb, you have two options in front of you to choose from i.e., 1 step or 2 step. This is a typical case of recursion where in you let the system decide the number of ways to climb the stairs by writing minimal code. The entire problem is broken down into sub-problems and you will encounter similar sub-problems. So to increase the efficiency of the solution, we can store the output of sub-probl

Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new  sorted  list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4  Output: 1->1->2->3->4->4   Solution: The approach here is similar to the "merge" step in Merge Sort . Time Complexity:  O(m+n) where m and n are length of two linked lists. Space Complexity: O(m+n) where m and n are length of two linked lists.

Valid Parentheses

Given a string containing just the characters  '(' ,  ')' ,  '{' ,  '}' ,  '['  and  ']' , determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered valid. Example 1: Input: "()" Output: true Example 2: Input: "()[]{}" Output: true Example 3: Input: "(]" Output: false Example 4: Input: "([)]" Output: false Example 5: Input: "{[]}"  Output: true   Solution: We will use stacks here.  Algorithm: 1) If the element encountered is '{' , '[' and '(', we will push it into stack. 2)  If the element encountered is ']','}' and ')', we will check if it's complement is present at the top of stack.  If the complement is not present then we will return false else we will repeat s

Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string  "" . Example 1: Input: ["flower","flow","flight"] Output: "fl" Example 2: Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. Note: All given inputs are in lowercase letters  a-z . Solution: The requirement here is to find longest common prefix amongst an array of strings.  There are many approaches to this problem but the one that I am going to discuss here uses Trie to store the array of strings and then scan the trie to get the longest common prefix. To know more about Trie, you can visit here . Algorithm: 1) Store each string from the array of strings in Trie. 2) Get the deepest Trie Node such that all the nodes from the root of the trie to that node has only one children. Space Complexity: O(n)

Roman To Integer

Roman numerals are represented by seven different symbols:  I ,  V ,  X ,  L ,  C ,  D  and  M . Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as  II  in Roman numeral, just two one's added together. Twelve is written as,  XII , which is simply  X  +  II . The number twenty seven is written as  XXVII , which is  XX  +  V  +  II . Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not  IIII . Instead, the number four is written as  IV . Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as  IX . There are six instances where subtraction is used: I  can be placed before  V  (5) and  X  (10) to make 4 and 9.  X  can be placed before  L  (50) and  C  (100) to make 40 and 90.  C  can be placed before  D  (500) and  M  (1000) to make 4

Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Could you solve it without converting the integer to a string?   Solution: One approach is to convert the number to string and then compare the string with it's reverse. Second approach which is also the answer to the follow-up question is to reverse the number and then compare the original and the reversed number. Time Complexity : O(log x) Space Complexity: O(1)