Word Search

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 cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. For example, Given board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] word = "ABCCED", -> returns true, word = "SEE", -> returns true, word = "ABCB", -> returns false. [Read more]

Wiggle Sort

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]….

For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4].

Wiggle Sort Solution

public class WiggleSort {
    public void wiggleSort(int[] nums) {
        for(int i = 1; i < nums.length; i++) {
            int current = nums[i - 1];

            if((i % 2 == 1) == (current > nums[i])) {
                nums[i - 1] = nums[i];
                nums[i] = current;
            }
        }
    }
}

Unique Paths

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

How many possible unique paths are there?

Unique Paths Solution

class UniquePaths {
    public int uniquePaths(int m, int n) {
        Integer[][] map = new Integer[m][n];

        //only 1 way to get to ith row, 0th column (move down)
        for(int i = 0; i < m; i++){
            map[i][0] = 1;
        }

        //only 1 way to get to ith column, 0th row (move right)
        for(int j= 0; j < n; j++){
            map[0][j]=1;
        }

        //x ways to get to ith row, jth column (# of ways to get to
        //ith - 1 row, jth column + # of ways to get to jth - 1 column
        //ith column
        for(int i = 1;i < m; i++){
            for(int j = 1; j < n; j++){
                map[i][j] = map[i - 1][j] + map[i][j - 1];
            }
        }

        return map[m - 1][n - 1];
    }
}

Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. Summary Ranges Solution public class SummaryRanges { public List<String> summaryRanges(int[] nums) { List<String> result = new ArrayList(); if(nums.length == 1) { result.add(nums[0] + ""); return result; } for(int i = 0; i < nums.length; i++) { int current = nums[i]; while(i + 1 < nums.length && (nums[i + 1] - nums[i] == 1)) { i++; } if(current ! [Read more]

Subsets II

Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example, If nums = [1,2,2], a solution is: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] Subsets Ii Solution public class SubsetsII { public List<List<Integer>> subsetsWithDup(int[] nums) { Arrays.sort(nums); List<List<Integer>> result = new ArrayList<List<Integer>>(); if(nums.length == 0 || nums == null) { return result; } helper(nums, new ArrayList<Integer>(), 0, result); return result; } public void helper(int[] nums, ArrayList<Integer> current, int index, List<List<Integer>> result) { result. [Read more]

Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. (http:sudoku.com.au/TheRules.aspx) The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’. A partially filled sudoku which is valid. Note: A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated. Valid Sudoku Solution class ValidSudoku { public boolean isValidSudoku(char[][] board) { for(int i = 0; i < board. [Read more]

Subsets

Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example, If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] Subsets Solution public class Subsets { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> result = new ArrayList<>(); recurse(result, nums, new Stack<>(), 0); return result; } private void recurse(List<List<Integer>> result, int[] nums, Stack path, int position) { if(position == nums. [Read more]

Valid Anagram

Valid Anagram Solution

class ValidAnagram {
    public boolean isAnagram(String s, String t) {
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        for(char c: s.toCharArray()) {
            if(map.containsKey(c)) {
                map.put(c, map.get(c) + 1);
            }
            else {
                map.put(c, 1);
            }
        }

        for(char c: t.toCharArray()) {
            if(map.containsKey(c)) {
                map.put(c, map.get(c) - 1);
            }
            else {
                return false;
            }
        }

        for(char c: map.keySet()) {
            if(map.get(c) != 0) {
                return false;
            }
        }

        return true;
    }
}

Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

For example,
Given n = 3,

You should return the following matrix:
[[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]]

Spiral Matrix Ii Solution

public class SpiralMatrix {
    public int[][] generateMatrix(int n) {
        int[][] spiral = new int[n][n];

        if(n == 0) {
            return spiral;
        }

        int rowStart = 0;
        int colStart = 0;
        int rowEnd = n - 1;
        int colEnd = n -1;
        int number = 1;

        while(rowStart <= rowEnd && colStart <= colEnd) {
            for(int i = colStart; i <= colEnd; i++) {
                spiral[rowStart][i] = number++;
            }

            rowStart++;

            for(int i = rowStart; i <= rowEnd; i++) {
                spiral[i][colEnd] = number++;
            }

            colEnd--;

            for(int i = colEnd; i >= colStart; i--) {
                if(rowStart <= rowEnd) {
                    spiral[rowEnd][i] = number++;
                }
            }

            rowEnd--;

            for(int i = rowEnd; i >= rowStart; i--) {
                if(colStart <= colEnd) {
                    spiral[i][colStart] = number++;
                }
            }

            colStart++;
        }

        return spiral;
    }
}

Valid Parentheses

Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not. Valid Parentheses Solution public class ValidParentheses { public boolean isValid(String s) { if(s.length() % 2 == 1) { return false; } Stack<Character> stack = new Stack<Character>(); for(int i = 0; i < s. [Read more]