Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for ‘?’ and ‘*’. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string . Example 1: Input: …
Input: 3 Output: , , ] Before looking into the solution, I recommend you to please go through the print matrix in spiral order solution. These two solutions are very similar. Solution in C++ #include<iostream> #include<vector> using namespace std; vector<vector<int> > generate_spiral_matrix(int n) { //Declaration of …
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL This problem can be solved in 2 ways. Iterative Recursive 1. Iterative solution. In this solution, we need 3 pointers. And initialize as shown below: prev = NULL Curr = head Temp = NULL So we …
You are given a collection of intervals, merge all overlapping intervals. Example 1: Input: ,,,] Output: ,,] Explanation: Since intervals and overlaps, merge them into . This problem can be solved in 2 ways: Brute force approach By sorting the input array 1. Brute Force approach …
Given an array of positive integers, you are at index 0, each element will represents maximum jump length. Check if you can reach to last index. Example 1: Input: Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last …
Example 1: Input: , , ] Output: The Solution is very simple. As shown in the image above, We traverse right We traverse down We traverse left We traverse up To achieve this, we take 4 variables: rowStart = 0; rowEnd = matrix.size()-1; colStart = 0; …
Example 1: Input: 2.00000, 10 Output: 1024.00000 Achieve it within O(log n) This problem can be solved in 4 different ways: Recursive Iterative Divide and Conquer Bit Manipulation We shall look at the first 3 methods. So let us understand how pow(x ^ n) means. It …
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. Input: Output: 6 Before solving this problem, please take a look at “Container with most water” problem. There I have explained …
Example: Input: , Output: , , ] This problem can be solved easily using Maps. The solution involves 2 steps: Sort the element and make it as the key. Take the value and place it in the key. In our example: Pass 1: …
Note: You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. Example 1: Given input matrix = , , ], rotate the input matrix in-place such that it becomes: …