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]

Unique Word Abbreviation

An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations: a) it --> it (no abbreviation) 1 b) d|o|g --> d1g 1 1 1 1---5----0----5--8 c) i|nternationalizatio|n --> i18n 1 1---5----0 d) l|ocalizatio|n --> l10n Assume you have a dictionary and given a word, find whether its abbreviation is unique in the dictionary. A word’s abbreviation is unique if no other word from the dictionary has the same abbreviation. [Read more]

Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] Output: [1,2,3,4,8,12,11,10,9,5,6,7] Spiral Matrix Solution class SpiralMatrix { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> result = new ArrayList<Integer>(); if(matrix == null || matrix. [Read more]

Valid Palindrome

Valid Palindrome Solution

public class ValidPalindrome {
    public boolean isPalindrome(String s) {
        int left = 0;
        int right = s.length() - 1;

        while(left < right) {
            while(!Character.isLetterOrDigit(s.charAt(left)) && left < right) {
                left++;
            }

            while(!Character.isLetterOrDigit(s.charAt(right)) && right > left) {
                right--;
            }

            if(Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
                return false;
            }

            left++;
            right--;
        }

        return true;
    }
}

Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. Two Sum Solution public class TwoSum { public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; HashMap<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < nums. [Read more]

Search In Rotated Sorted Array

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. // (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. Search In Rotated Sorted Array Solution public class SearchInRotatedSortedArray { public int search(int[] nums, int target) { int left = 0; int right = nums. [Read more]