Posts

Maximal Square(dp,cpp,leetcode)

Maximal Square (dp,cpp,leetcode) Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. Example: Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4    problem link====== https://leetcode.com/problems/maximal-square/ ========================================================================================== class Solution { public: int maximalSquare(vector<vector<char>>& matrix) { int maxm=0; if(matrix.size()==0) return 0; int t[matrix.size()][matrix[0].size()]; memset(t,0,sizeof(t)); for(int i=0;i<matrix.size();i++) { t[i][0]=matrix[i][0]-48; if(maxm<t[i][0]) maxm=t[i][0]; } for(int i=0;i<matrix[0].size();i++) { t[0][i]=matrix[0][i]-48; if(maxm<t[0][i]) maxm=t[0][i]; } ...

Edit Distance(leetcode, cpp,dp)

Edit Distance(leetcode, cpp,dp)  ===============================================   Given two words word1 and word2 , find the minimum number of operations required to convert word1 to word2 . You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1: Input: word1 = "horse", word2 = "ros" Output: 3 Explanation: horse -> rorse (replace 'h' with 'r') rorse -> rose (remove 'r') rose -> ros (remove 'e')  =================================================== class Solution { public:     int minDistance(string w1, string w2)     {         int t[w1.size()+1][w2.size()+1];         for(int  i=0;i<w2.size()+1;i++)         {             t[0][i]=i;         }  ...

Amazing Subarrays(cpp,interviewbit)

 Amazing subarray(cpp,interviewbit) You are given a string S , and you have to find all the amazing substrings of S . Amazing Substring is one that starts with a vowel (a, e, i, o, u, A, E, I, O, U). ================================================= Example Input ABEC Output 6 Explanation Amazing substrings of given string are : 1. A 2. AB 3. ABE 4. ABEC 5. E 6. EC here number of substrings are 6 and 6 % 10003 = 6. =========================================================================================== int Solution::solve(string s) { long int count=0; long int x=0; unordered_map<char,int>m={{'a',1},{'e',1},{'i',1},{'o',1},{'u',1},{'A',1},{'E',1},{'O',1},{'I',1},{'U',1}}; for(long int i=s.size()-1;i>=0;i--) { x++; if(m.find(s[i]) != m.end()) { count=count+x; } } return count%100...

3Sum(cpp,leetcode)

3Sum(cpp,leetcode) Given an array nums of n integers, are there elements a , b , c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Notice that the solution set must not contain duplicate triplets. Example 1: Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]]  ================================================================================ class Solution { public: vector<vector<int>> threeSum(vector<int>& a) { sort(a.begin(),a.end()); int left,right; vector<vector<int>>v; if(a.size()<3) { return v; } // left=1; // right=a.size()-1; for(int i=0;i<a.size()-2;i++) { int t=a[i]; left=i+1; right=a.size()-1; if(i>0 && a[i]==a[i-1]) continue; ...

Palindrome Linked List(cpp)

  Palindrome Linked List(cpp) Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Output: true ========================================================================================= class Solution { public: bool isPalindrome(ListNode* head) { stack<int>st; ListNode *temp; temp=head; while(temp) { st.push(temp->val); temp=temp->next; } temp=head; while(temp) { int x=st.top(); st.pop(); if(x!=temp->val) return false; temp=temp->next; } return true; } };  

converts a string to an integer.(cpp,leetcode)

converts a string to an integer. ============================================== The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. Note: Only the space character ' ' is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2 31 ,  2 31  − 1]. If the numerical value is out of the range of representable values, INT_MAX (2 31  − 1) or INT_MIN (−2 31 ) is returned.   =============================================== Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign.   Then take as many numer...

Invert Binary Tree into its mirror tree.(cpp,leetcode)

Invert Binary Tree into its mirror tree.(cpp,leetcode)   input: 4 / \ 2 7 / \ / \ 1 3 6 9   output: 4 / \ 7 2 / \ / \ 9 6 3 1 =============================================== code:-   class Solution { public:     void xx(TreeNode *root)     {          if(root==NULL)             return ;         else         {             invertTree(root->left);             invertTree(root->right);             swap(root->left,root->right);                 ...