Wildcard Matching

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 (not partial). Note: s could be empty and contains only lowercase letters a-z. […]

Reverse Linked List iterative and recursive in C++

Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL   This problem can be solved in 2 ways. 1. Iterative 2. Recursive. 1. Iterative solution. In this solution, we need 3 pointers. In this solution, we need 3 pointers. And initialize as shown below: prev = NULL Curr = head Temp = NULL So we iterate and reverse the […]

Merge Intervals in C++

Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. Example 2: Input: [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4] and [4,5] are considered overlapping. This problem can be solved in 2 ways: 1. Brute force approach 2. By sorting […]

Implement pow(x, n), which calculates x raised to the power n (xn) in C++

Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 Output: 9.26100 Example 3: Input: 2.00000, -2 Output: 0.25000 Explanation: 2-2 = 1/22 = 1/4 = 0.25 Achieve it with in O(long n) This problem can be solved in 4 different ways: 1. Recursive 2. Iterative 3. Divide and Conquer 4. Bit Manipulation […]

Rain water trapping

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: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Before solving this problem, please take a look at “Container with most water” problem. There I have explained in detail on how two pointers […]

Group Anagrams in C++

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"] Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] • All inputs will be in lowercase. • The order of your output does not matter. This problem can be solved easily using Maps. Solution involves 2 steps: 1. Sort the element and […]

You are given an n x n 2D matrix rotate it by 90 degrees (clockwise) in C++.

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 = [ [1,2,3], [4,5,6], [7,8,9] ], rotate the input matrix in-place such that it becomes: [ [7,4,1], [8,5,2], [9,6,3] ] We […]