## Return matrix in Spiral order (Java)

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, given the following matrix: [ [ 1,…

## Shortest Distance from All Buildings (Java)

Java Solution This problem can be solved by BFS. We define one matrix for tracking the distance from each building, and another matrix for tracking the number of buildings that…

## Search a 2D Matrix (Java)

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has properties: 1) Integers in each row are sorted from left to right.…

## Minimum Path Sum in Matrix (Java)

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Java…

## Tic-Tac-Toe (Java)

Design a Tic-tac-toe game that is played between two players on a n x n grid. Java Solution 1 - Naive We can simply check the row, column, and diagonals…

## Sudoku Solver (Java)

Write a program to solve a Sudoku puzzle by filling the empty cells. Java Solution public void solveSudoku(char[][] board) { helper(board); } private boolean helper(char[][] board){ for(int i=0; i<9; i++){…

## Find if the word exists in the grid (Java)

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where "adjacent" cells are…

## Find the largest rectangle in Matrix (Java)

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. Java Solution public int maximalRectangle(char[][] matrix) { int m…

## Find the largest square in Matrix (Java)

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1101 1101 1111…

## Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()"…