Category: fullstackdevelopment
-
Data Structures & Algorithms in Java – Matrix – Spiral Matrix
Problem: Given a matrix , collect the elements in the matrix in a spiral direction clockwise and return them as a list. For example: Consider the matrix [[1,2,3],[4,5,6],[7,8,9]] You need to traverse the matrix spirally like below: So the output is : [1,2,3,6,9,8,7,4,5] Try out the solution here: https://leetcode.com/problems/spiral-matrix/ Solution: Hint: Traverse right , then…
-
Data Structures & Algorithms in Java – Strings – Encode and Decode Strings
Problem: Given a list of strings , encode them and then decode them back. Consider the use case where you want to send a list of strings over the network. You have the limitation of using only string data structure. And somehow you need to represent the list of strings as a string and then…
-
Data Structures & Algorithms in Java – Linked List – Merge k sorted lists.
Problem: Given k singly linked sorted lists merge them together into one sorted list. For example, given the below lists: [[1,4,5],[1,3,4],[2,6]] merge them into [1,1,2,3,4,4,5,6] Try out the solution here: https://leetcode.com/problems/merge-k-sorted-lists/ Solution: Hint: Let’s look at all of them . Brute force In this case , you copy all the node values into an array.…
-
Data Structures & Algorithms in Java – Strings – Valid Parantheses
Problem: Given a string containing parantheses ( (,).{,},[,]) check if the parantheses match or not. For example, The string “()[]{}” is a valid string because the opening parentheses are matched by closing ones. The string “{[()]}” is also a valid expression since all the opening parentheses have corresponding closing parentheses The string “(]” is invalid…
-
Data Structures & Algorithms in Java – Strings – Minimum Window Substring
Problem: Given two strings s and t , find the minimum window substring from s which contains all the characters of the string t. For example, For the input strings s = “ADOBECODEBANC” t = “ABC” The minimum window substring in s is “BANC” The possible substrings are “ADOBEC” , “BECODEBA” , “CODEBA” and “BANC”…
-
Data Structures & Algorithms in Java – Dynamic Programming – Unique Paths
Problem: Given a 2 dimensional matrix size m * n , find the number of unique ways to reach the bottom right of the matrix from the top left of the matrix (first cell) if you can move only right or down a step at a time . That is from input[0][0] to input[m-1][n-1] For…
-
Data Structures & Algorithms in Java – Dynamic Programming – Decode Ways
Problem: If A is encoded as 1 , B as 2 and so on until Z as 26 , then find the number of ways a given encoded string can be decoded. For example: The input “121” can be decoded as 1, 2 ,1 (ABA) or 12 1(LA) or 1 21 (AU) So the output…
-
Data Structures and Algorithms in Java – Dynamic Programming – Combination Sum
Problem: Given an array of numbers and a target number , find in how many ways the numbers in the input array can be summed up to form the target number. For example, Given the input: [1,2,3] and the target 4 The output should be 7 since : 1 + 1 + 1 + 1…
-
Data Structures & Algorithms in Java – Dynamic Programming – House Robber II
Problem: A robber goes to rob in a series of houses built next to each other forming a circle. So the first house and last house are next to each other. The robber cannot steal from consecutive houses as it would trigger an alarm. Find the maximum money the robber can steal. Input: [2,3,2] Each…
-
Datastructures & Algorithms in Java – Dynamic Programming – Word Break
Problem: Given a word , find if the word could be formed by joining words together from a given dictionary. Example: The word “datastructure” can be formed using the words from the dictionary: [“data”,”algorithms”,”structure”] So ,the output is true. For the word “breadandbiscuit” and the dictionary [“and”,”bis”,”bread”] the output is false Assumptions: The words and…