Posts

Showing posts with the label #two-pointer

Strings - Backspace String Compare using JAVA Stack

Question: Given two Strings S and T, return true if they equal when both are typed out any "#" will be treated as backspace. input - S -> ab#c, T -> az#c output - True Constraints need to ask because solution may change according to given constraints Question: What happens when 2 or multiple "#" appear besides each other? Answer: Delete same number of values before 1st "#". Ex- ab## -> "" Question: What happen to "#" when there is no character to remove? Answer: It deletes nothing. Ex- a###b -> "b" Question: Are two empty Strings equals each other? Answer: Ex- S -> a#b#c#, T -> c#. Both returns empty string and output will be true. Question: Does case sensitivity matters? Answer: Yes. Ex- A is different from a. S -> ab#c, T -> Az#c. Output - False Solution using JAVA language. BruteForce Approach - This is simple approach, We are using JAVA Stack here,...

Arrays - Trapping Rainwater - Optimal Solution using 2 Pointer Approach

Image
Question: Given an array of integers representing an elevation map (bar chart) where width of each bar is 1, return how much rain water can be trapped in empty space after raining? input [0,1,0,2,1,0,3,1,0,1,2] output 8 Constraints need to ask because solution may change according to given constraints Question: Sides of graph can be count as wall? Answer: No Question: Will there be negative integers? Answer: No Question: What will be output for empty array? Answer: 0 Question: What will be output when only 1 value in array? Answer: 0 Question: What will be output when input array like [3,4,3]? Answer: 0, no empty space to store water because walls cannot be used and water also fells from right side. Note: Max water can go to the mark is minimum height of bar between 2 bars and if there is a small bar pre...

Arrays - Container with Most Water

Image
Question: Given an array of positive numbers where each number represent the height of a vertical line on a chart. Find two lines which together with the x-axis forms a container that would hold greatest amount of water. what will be the output which is max area of container formed by vertical lines. input [8, 2, 1, 5, 6, 9] output 40 Constraints need to ask because solution may change according to given constraints Question: Does the thickness of lines affect the are? Answer: No Question: Sides of graph can be count as wall? Answer: No Question: Does a higher line inside our container affect our area? Answer: No Question: What will be output for empty array? Answer: 0 Question: What will be output when only 1 value in array? Answer: 0 ...