Skip to content

Instantly share code, notes, and snippets.

@anil477
anil477 / RestoreIPAddresses.java
Created January 30, 2022 14:46
93. Restore IP Addresses
class Solution {
// 93. Restore IP Addresses
// https://leetcode.com/problems/restore-ip-addresses/
public List<String> restoreIpAddresses(String rawIpString) {
List<String> restoredIps = new ArrayList<>();
restoreIps(0, 0, new int[4], rawIpString, restoredIps);
return restoredIps;
@anil477
anil477 / Word Search.java
Created January 30, 2022 09:17
79. Word Search
public class Solution {
// 79. Word Search
// https://leetcode.com/problems/word-search/
public boolean exist(char[][] board, String word) {
for(int i = 0; i < board.length; i++)
for(int j = 0; j < board[0].length; j++){
if (board[i][j] == word.charAt(0)) {
if(exist(board, i, j, word, 0))
@anil477
anil477 / SudokuSolver.java
Created January 30, 2022 08:48
37. Sudoku Solver
public class Solution {
// 37. Sudoku Solver
// https://leetcode.com/problems/sudoku-solver/
public void solveSudoku(char[][] board) {
if(board == null || board.length == 0)
return;
solve(board);
}
public boolean solve(char[][] board){
@anil477
anil477 / GenerateParentheses.java
Created January 30, 2022 06:53
22. Generate Parentheses
class Solution {
// 22. Generate Parentheses
// https://leetcode.com/problems/generate-parentheses/
public List<String> generateParenthesis(int n) {
List<String> ans = new ArrayList();
backtrack(ans, new StringBuilder(), 0, 0, n);
return ans;
}
@anil477
anil477 / LetterCombinationsofPhoneNumber.java
Created January 29, 2022 19:48
17. Letter Combinations of a Phone Number
class Solution {
// 17. Letter Combinations of a Phone Number
// https://leetcode.com/problems/letter-combinations-of-a-phone-number/
// https://leetcode.com/problems/letter-combinations-of-a-phone-number/discuss/8109/My-recursive-solution-using-Java/222635
private static final String[] KEYS = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
public List<String> letterCombinations(String digits) {
List<String> ret = new LinkedList<String>();
@anil477
anil477 / CombinationSumII.java
Created January 29, 2022 14:05
40. Combination Sum II
class Solution {
// https://leetcode.com/problems/combination-sum-ii/
// 40. Combination Sum II
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(candidates);
boolean[] used = new boolean[candidates.length];
@anil477
anil477 / CombinationSum.java
Created January 29, 2022 13:58
39. Combination Sum
class Solution {
// 39. Combination Sum
// https://leetcode.com/problems/combination-sum/
public List<List<Integer>> combinationSum(int[] nums, int target) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, target, 0);
return list;
@anil477
anil477 / SubsetsII.java
Created January 29, 2022 12:28
90. Subsets II
class Solution {
// https://leetcode.com/problems/subsets-ii/
// 90. Subsets II
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> output = new LinkedList();
boolean[] used = new boolean[nums.length];
Arrays.sort(nums);
rec(nums, output, new ArrayList<>(), 0, used);
@anil477
anil477 / Subsets.java
Last active January 29, 2022 12:21
78. Subsets
class Solution {
// 78. Subsets
// https://leetcode.com/problems/subsets/
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> output = new LinkedList();
rec(nums, output, new ArrayList<>(), 0);
// Arrays.sort(nums);
return output;
@anil477
anil477 / PermutationsII.java
Last active January 29, 2022 11:49
47. Permutations II
public class Solution {
// https://leetcode.com/problems/permutations-ii/
// 47. Permutations II
// https://leetcode.com/problems/permutations/discuss/18239/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partioning)
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(nums==null || nums.length==0) return res;
boolean[] used = new boolean[nums.length];
List<Integer> list = new ArrayList<Integer>();