Example, path = "/home/", => "/home" path = "/x/./y/../../z/", => "/z" Corner Cases: Did you consider the case where path = “/../”? In this case, you should return “/”. Another corner case is the path might contain multiple slashes ‘/’ together, such as “/home//foo/”. In this case, you should ignore redundant slashes and return “/home/foo”. To solve this problem, …
You are given a m x n matrix with elements which represents cost. your goal is to start from top left and reach to bottom right with minimum cost. You can move only down or right. Example: Input: , , ] Output: 7 Minimum cost path …
Problem Explanation: A bike is located at the top-left corner of a m x n matrix. Bike can only move either down or right at any point in time. Bike is trying to reach the bottom-right corner of the matrix. There are some obstacles along the way, find the …
Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3->4->NULL rotate 2 steps to the right: 4->5->1->2->3->NULL Algorithm: Step 1: Take 2 pointers pointing to the head pointer. new_head = tail = head Step 2: Get the length of the …
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 …