LeetCode – 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++){ for(int j=0; j<9; j++){ if(board[i][j]!=’.’){ continue; }   for(char k=’1′; k<=’9′; k++){ board[i][j]=k; if(isValid(board, i, j) && helper(board)){ return true; } board[i][j]=’.’; }   … Read more

LeetCode – Remove Nth Node From End of List (Java)

Given a linked list, remove the nth node from the end of list and return its head. For example, given linked list 1->2->3->4->5 and n = 2, the result is 1->2->3->5. Java Solution 1 – Naive Two Passes Calculate the length first, and then remove the nth from the beginning. public ListNode removeNthFromEnd(ListNode head, int … Read more

LeetCode – Add and Search Word – Data structure design (Java)

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter. Java Solution 1 This problem is similar with Implement Trie. The solution 1 below uses … Read more

LeetCode – Remove Invalid Parentheses (Java)

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Examples: “()())()” -> [“()()()”, “(())()”] “(a)())()” -> [“(a)()()”, “(a())()”] “)(” -> [“”] Java Solution This problem can be solve by using DFS. … Read more