From abdea62b8c43af6f144074d921706abd944ae49d Mon Sep 17 00:00:00 2001 From: Arun Mishra Date: Sat, 19 Oct 2024 18:17:15 +0530 Subject: [PATCH 1/2] Problem 13 Solved --- Microsoft/problem13.md | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/Microsoft/problem13.md b/Microsoft/problem13.md index 276e1d4..e601f02 100644 --- a/Microsoft/problem13.md +++ b/Microsoft/problem13.md @@ -25,3 +25,56 @@ Constraints: - 1 <= s.length <= 1000 - s consist of only digits and English letters. + + +/// C++ Solution + +class Solution { +public: + string longestPalindrome(string s) { + + if(s.length() <= 1) return s; + int max_len = 1; + int n = s.length(); + int st = 0, end = 0; + + // Odd length + for(int i = 0; i < n-1; ++i){ + int l = i, r = i; + while(l >= 0 && r < n){ + if(s[l] == s[r]){ + l--; r++; + }else + break; + } + int len = r-l-1; + if(len > max_len){ + max_len = len; + st = l+1; + end = r-1; + } + } + + // Even length + for(int i = 0; i < n-1; ++i){ + int l = i, r = i+1; + while(l >= 0 && r < n){ + if(s[l] == s[r]){ + l--; r++; + }else + break; + } + int len = r-l-1; + if(len > max_len){ + max_len = len; + st = l+1; + end = r-1; + } + } + + return s.substr(st, max_len); + + } +}; + + From eb41bbe01e159868e7e3e154203e894d1b6ec190 Mon Sep 17 00:00:00 2001 From: Arun Mishra Date: Sat, 19 Oct 2024 23:53:22 +0530 Subject: [PATCH 2/2] Problem 9 Solved --- JP Morgan/problem9.md | 66 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/JP Morgan/problem9.md b/JP Morgan/problem9.md index 73e8cb2..d6cd9d2 100644 --- a/JP Morgan/problem9.md +++ b/JP Morgan/problem9.md @@ -2,33 +2,29 @@ ## Problem Description - Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. - - Example 1: Input: grid = [ - ["1","1","1","1","0"], - ["1","1","0","1","0"], - ["1","1","0","0","0"], - ["0","0","0","0","0"] +["1","1","1","1","0"], +["1","1","0","1","0"], +["1","1","0","0","0"], +["0","0","0","0","0"] ] Output: 1 Example 2: Input: grid = [ - ["1","1","0","0","0"], - ["1","1","0","0","0"], - ["0","0","1","0","0"], - ["0","0","0","1","1"] +["1","1","0","0","0"], +["1","1","0","0","0"], +["0","0","1","0","0"], +["0","0","0","1","1"] ] Output: 3 - Constraints: @@ -37,3 +33,49 @@ Constraints: - 1 <= m, n <= 300 - grid[i][j] is '0' or '1'. +/// C++ Solution + +class Solution { +public: + + void bfs(int row,int col,vector> &vis,vector>&grid){ + vis[row][col]=1; + queue> q; + q.push({row,col}); + int n = grid.size(); + int m = grid[0].size(); + vector> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + + while(!q.empty()){ + int row = q.front().first; + int col = q.front().second; + q.pop(); + for (auto [dr,dc] : directions){ + int nrow = row + dr ; + int ncol = col + dc ; + if(nrow >=0 && nrow < n && ncol >= 0 && ncol >& grid) { + int n = grid.size(); + int m = grid[0].size(); + vector>vis(n,vector(m,0)); + int count = 0 ; + for(int row = 0 ; row