diff --git a/README.md b/README.md index 89473655c0..85f52b63e3 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome! | # | Title | Solutions | Time | Space | Video | Difficulty | Tag |-----|----------------|---------------|---------------|---------------|--------|-------------|------------- +|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) | O(n) | O(1) | |Easy|| |1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java) | O(n) | O(m) | |Easy|| |1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java) | O(1) | O(1) | |Easy|| |1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java) | O(n) | O(n) | |Easy|| diff --git a/src/main/java/com/fishercoder/common/utils/CommonUtils.java b/src/main/java/com/fishercoder/common/utils/CommonUtils.java index e969aa8f96..87fd300b31 100644 --- a/src/main/java/com/fishercoder/common/utils/CommonUtils.java +++ b/src/main/java/com/fishercoder/common/utils/CommonUtils.java @@ -221,4 +221,14 @@ public static void printArrayArray(int[][] arrayArrays) { } System.out.println(); } + + public static void print2DCharArray(char[][] arrayArrays) { + for (char[] array : arrayArrays) { + for (char i : array) { + System.out.print(i + ", "); + } + System.out.println(); + } + System.out.println(); + } } diff --git a/src/main/java/com/fishercoder/solutions/_1207.java b/src/main/java/com/fishercoder/solutions/_1207.java new file mode 100644 index 0000000000..0d432c54fe --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/_1207.java @@ -0,0 +1,43 @@ +package com.fishercoder.solutions; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * 1207. Unique Number of Occurrences + * + * Given an array of integers arr, + * write a function that returns true if and only if the number of occurrences of each value in the array is unique. + * + * Example 1: + * Input: arr = [1,2,2,1,1,3] + * Output: true + * Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences. + * + * Example 2: + * Input: arr = [1,2] + * Output: false + * + * Example 3: + * Input: arr = [-3,0,1,-3,1,1,1,-3,10,0] + * Output: true + * + * Constraints: + * 1 <= arr.length <= 1000 + * -1000 <= arr[i] <= 1000 + * */ +public class _1207 { + public static class Solution1 { + public boolean uniqueOccurrences(int[] arr) { + Map map = new HashMap<>(); + Arrays.stream(arr).forEach(num -> { + map.put(num, map.containsKey(num) ? map.get(num) + 1 : 1); + }); + Set set = new HashSet<>(); + return map.keySet().stream().mapToInt(key -> key).allMatch(key -> set.add(map.get(key))); + } + } +} diff --git a/src/main/java/com/fishercoder/solutions/_24.java b/src/main/java/com/fishercoder/solutions/_24.java index 5d04c4e311..e7b07e484c 100644 --- a/src/main/java/com/fishercoder/solutions/_24.java +++ b/src/main/java/com/fishercoder/solutions/_24.java @@ -14,16 +14,17 @@ */ public class _24 { - - public ListNode swapPairs(ListNode head) { - if (head == null || head.next == null) { - return head; + public static class Solution1 { + public ListNode swapPairs(ListNode head) { + if (head == null || head.next == null) { + return head; + } + ListNode second = head.next; + ListNode third = second.next; + second.next = head; + head.next = swapPairs(third); + return second; } - ListNode second = head.next; - ListNode third = second.next; - second.next = head; - head.next = swapPairs(third); - return second; } } diff --git a/src/main/java/com/fishercoder/solutions/_35.java b/src/main/java/com/fishercoder/solutions/_35.java index 318db230dc..ea88dc9669 100644 --- a/src/main/java/com/fishercoder/solutions/_35.java +++ b/src/main/java/com/fishercoder/solutions/_35.java @@ -18,14 +18,16 @@ public class _35 { - public int searchInsert(int[] nums, int target) { - int len = nums.length; - for (int i = 0; i < len; i++) { - if (target <= nums[i]) { - return i; + public static class Solution1 { + public int searchInsert(int[] nums, int target) { + int len = nums.length; + for (int i = 0; i < len; i++) { + if (target <= nums[i]) { + return i; + } } + return len; } - return len; } } diff --git a/src/main/java/com/fishercoder/solutions/_37.java b/src/main/java/com/fishercoder/solutions/_37.java index 92d4c15282..3f7fcbf8c5 100644 --- a/src/main/java/com/fishercoder/solutions/_37.java +++ b/src/main/java/com/fishercoder/solutions/_37.java @@ -3,9 +3,53 @@ /** * 37. Sudoku Solver * - * Write a program to solve a Sudoku puzzle by filling the empty cells. - * Empty cells are indicated by the character '.'. - * You may assume that there will be only one unique solution. + * Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: + * + * Each row must contain the digits 1-9 without repetition. + * Each column must contain the digits 1-9 without repetition. + * Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition. + * + * The Sudoku board could be partially filled, where empty cells are filled with the character '.'. + * + * Example 1: + * + * Input: + * [ + * ["5","3",".",".","7",".",".",".","."], + * ["6",".",".","1","9","5",".",".","."], + * [".","9","8",".",".",".",".","6","."], + * ["8",".",".",".","6",".",".",".","3"], + * ["4",".",".","8",".","3",".",".","1"], + * ["7",".",".",".","2",".",".",".","6"], + * [".","6",".",".",".",".","2","8","."], + * [".",".",".","4","1","9",".",".","5"], + * [".",".",".",".","8",".",".","7","9"] + * ] + * Output: true + * Example 2: + * + * Input: + * [ + * ["8","3",".",".","7",".",".",".","."], + * ["6",".",".","1","9","5",".",".","."], + * [".","9","8",".",".",".",".","6","."], + * ["8",".",".",".","6",".",".",".","3"], + * ["4",".",".","8",".","3",".",".","1"], + * ["7",".",".",".","2",".",".",".","6"], + * [".","6",".",".",".",".","2","8","."], + * [".",".",".","4","1","9",".",".","5"], + * [".",".",".",".","8",".",".","7","9"] + * ] + * Output: false + * Explanation: Same as Example 1, except with the 5 in the top left corner being + * modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid. + * Note: + * + * A Sudoku board (partially filled) could be valid but is not necessarily solvable. + * Only the filled cells need to be validated according to the mentioned rules. + * The given board contain only digits 1-9 and the character '.'. + * The given board size is always 9x9. + * */ public class _37 { diff --git a/src/test/java/com/fishercoder/_1207Test.java b/src/test/java/com/fishercoder/_1207Test.java new file mode 100644 index 0000000000..8b3e326fac --- /dev/null +++ b/src/test/java/com/fishercoder/_1207Test.java @@ -0,0 +1,35 @@ +package com.fishercoder; + +import com.fishercoder.solutions._1207; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class _1207Test { + private static _1207.Solution1 solution1; + private static int[] arr; + + @BeforeClass + public static void setup() { + solution1 = new _1207.Solution1(); + } + + @Test + public void test1() { + arr = new int[]{1, 2, 2, 1, 1, 3}; + assertEquals(true, solution1.uniqueOccurrences(arr)); + } + + @Test + public void test2() { + arr = new int[]{1, 2}; + assertEquals(false, solution1.uniqueOccurrences(arr)); + } + + @Test + public void test3() { + arr = new int[]{-3, 0, 1, -3, 1, 1, 1, -3, 10, 0}; + assertEquals(true, solution1.uniqueOccurrences(arr)); + } +} diff --git a/src/test/java/com/fishercoder/_24Test.java b/src/test/java/com/fishercoder/_24Test.java new file mode 100644 index 0000000000..98a53a8aa5 --- /dev/null +++ b/src/test/java/com/fishercoder/_24Test.java @@ -0,0 +1,26 @@ +package com.fishercoder; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions._24; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.Arrays; + +public class _24Test { + private static _24.Solution1 solution1; + private static ListNode head; + + @BeforeClass + public static void setup() { + solution1 = new _24.Solution1(); + } + + @Test + public void test1() { + head = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4)); + CommonUtils.printList(solution1.swapPairs(head)); + } + +} \ No newline at end of file diff --git a/src/test/java/com/fishercoder/_29Test.java b/src/test/java/com/fishercoder/_29Test.java new file mode 100644 index 0000000000..9f9ebc10c7 --- /dev/null +++ b/src/test/java/com/fishercoder/_29Test.java @@ -0,0 +1,21 @@ +package com.fishercoder; + +import com.fishercoder.solutions._29; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class _29Test { + private static _29.Solution1 solution1; + + @BeforeClass + public static void setup() { + solution1 = new _29.Solution1(); + } + + @Test + public void test1() { + assertEquals(1, solution1.divide(4, 3)); + } +} diff --git a/src/test/java/com/fishercoder/_31Test.java b/src/test/java/com/fishercoder/_31Test.java new file mode 100644 index 0000000000..98ee63a8e1 --- /dev/null +++ b/src/test/java/com/fishercoder/_31Test.java @@ -0,0 +1,23 @@ +package com.fishercoder; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions._31; +import org.junit.BeforeClass; +import org.junit.Test; + +public class _31Test { + private static _31.Solution1 solution1; + private static int[] nums; + + @BeforeClass + public static void setup() { + solution1 = new _31.Solution1(); + } + + @Test + public void test1() { + nums = new int[]{1, 2, 3}; + solution1.nextPermutation(nums); + CommonUtils.printArray(nums); + } +} diff --git a/src/test/java/com/fishercoder/_32Test.java b/src/test/java/com/fishercoder/_32Test.java new file mode 100644 index 0000000000..c1db3cfae9 --- /dev/null +++ b/src/test/java/com/fishercoder/_32Test.java @@ -0,0 +1,21 @@ +package com.fishercoder; + +import com.fishercoder.solutions._32; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class _32Test { + private static _32.Solution1 solution1; + + @BeforeClass + public static void setup() { + solution1 = new _32.Solution1(); + } + + @Test + public void test1() { + assertEquals(2, solution1.longestValidParentheses("(()")); + } +} diff --git a/src/test/java/com/fishercoder/_33Test.java b/src/test/java/com/fishercoder/_33Test.java new file mode 100644 index 0000000000..aa0e6f14cd --- /dev/null +++ b/src/test/java/com/fishercoder/_33Test.java @@ -0,0 +1,26 @@ +package com.fishercoder; + +import com.fishercoder.solutions._33; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class _33Test { + private static _33.Solution1 solution1; + private static _33.Solution2 solution2; + private static int[] nums; + + @BeforeClass + public static void setup() { + solution1 = new _33.Solution1(); + solution2 = new _33.Solution2(); + } + + @Test + public void test1() { + nums = new int[]{4, 5, 6, 7, 0, 1, 2}; + assertEquals(3, solution1.search(nums, 7)); + assertEquals(3, solution2.search(nums, 7)); + } +} diff --git a/src/test/java/com/fishercoder/_35Test.java b/src/test/java/com/fishercoder/_35Test.java new file mode 100644 index 0000000000..3062dcd46a --- /dev/null +++ b/src/test/java/com/fishercoder/_35Test.java @@ -0,0 +1,23 @@ +package com.fishercoder; + +import com.fishercoder.solutions._35; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class _35Test { + private static _35.Solution1 solution1; + private static int[] nums; + + @BeforeClass + public static void setup() { + solution1 = new _35.Solution1(); + } + + @Test + public void test1() { + nums = new int[]{1, 3, 5, 6}; + assertEquals(2, solution1.searchInsert(nums, 5)); + } +} diff --git a/src/test/java/com/fishercoder/_37Test.java b/src/test/java/com/fishercoder/_37Test.java new file mode 100644 index 0000000000..c058a24a6b --- /dev/null +++ b/src/test/java/com/fishercoder/_37Test.java @@ -0,0 +1,34 @@ +package com.fishercoder; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions._37; +import org.junit.BeforeClass; +import org.junit.Test; + +public class _37Test { + private static _37.Solution1 solution1; + private static char[][] board; + + @BeforeClass + public static void setup() { + solution1 = new _37.Solution1(); + } + + @Test + public void test1() { + board = new char[][]{ + {'5', '3', '.', '.', '7', '.', '.', '.', '.'}, + {'6', '.', '.', '1', '9', '5', '.', '.', '.'}, + {'.', '9', '8', '.', '.', '.', '.', '6', '.'}, + {'8', '3', '.', '.', '6', '.', '.', '.', '3'}, + {'4', '.', '.', '8', '.', '3', '.', '.', '1'}, + {'7', '.', '.', '.', '2', '.', '.', '.', '6'}, + {'.', '6', '.', '.', '7', '.', '2', '8', '.'}, + {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, + {'.', '.', '.', '.', '8', '.', '.', '7', '9'} + }; + CommonUtils.print2DCharArray(board); + solution1.solveSudoku(board); + CommonUtils.print2DCharArray(board); + } +} diff --git a/src/test/java/com/fishercoder/_38Test.java b/src/test/java/com/fishercoder/_38Test.java new file mode 100644 index 0000000000..d2d2644546 --- /dev/null +++ b/src/test/java/com/fishercoder/_38Test.java @@ -0,0 +1,21 @@ +package com.fishercoder; + +import com.fishercoder.solutions._38; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class _38Test { + private static _38.Solution1 solution1; + + @BeforeClass + public static void setup() { + solution1 = new _38.Solution1(); + } + + @Test + public void test1() { + assertEquals("21", solution1.countAndSay(3)); + } +} diff --git a/src/test/java/com/fishercoder/_46Test.java b/src/test/java/com/fishercoder/_46Test.java new file mode 100644 index 0000000000..506567ecc5 --- /dev/null +++ b/src/test/java/com/fishercoder/_46Test.java @@ -0,0 +1,20 @@ +package com.fishercoder; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions._46; +import org.junit.BeforeClass; +import org.junit.Test; + +public class _46Test { + private static _46.Solution1 solution1; + + @BeforeClass + public static void setup() { + solution1 = new _46.Solution1(); + } + + @Test + public void test1() { + CommonUtils.printListList(solution1.permute(new int[]{1, 2, 3})); + } +} diff --git a/src/test/java/com/fishercoder/_58Test.java b/src/test/java/com/fishercoder/_58Test.java new file mode 100644 index 0000000000..9dca450c7a --- /dev/null +++ b/src/test/java/com/fishercoder/_58Test.java @@ -0,0 +1,21 @@ +package com.fishercoder; + +import com.fishercoder.solutions._58; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class _58Test { + private static _58.Solution1 solution1; + + @BeforeClass + public static void setup() { + solution1 = new _58.Solution1(); + } + + @Test + public void test1() { + assertEquals(5, solution1.lengthOfLastWord("Hello World")); + } +} diff --git a/src/test/java/com/fishercoder/_59Test.java b/src/test/java/com/fishercoder/_59Test.java new file mode 100644 index 0000000000..dc31098345 --- /dev/null +++ b/src/test/java/com/fishercoder/_59Test.java @@ -0,0 +1,22 @@ +package com.fishercoder; + +import com.fishercoder.common.utils.CommonUtils; +import com.fishercoder.solutions._59; +import org.junit.BeforeClass; +import org.junit.Test; + +public class _59Test { + private static _59.Solution1 solution1; + private static int[][] matrix; + + @BeforeClass + public static void setup() { + solution1 = new _59.Solution1(); + } + + @Test + public void test1() { + matrix = solution1.generateMatrix(6); + CommonUtils.print2DIntArray(matrix); + } +} diff --git a/src/test/java/com/fishercoder/_60Test.java b/src/test/java/com/fishercoder/_60Test.java new file mode 100644 index 0000000000..0c28e11bb6 --- /dev/null +++ b/src/test/java/com/fishercoder/_60Test.java @@ -0,0 +1,21 @@ +package com.fishercoder; + +import com.fishercoder.solutions._60; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class _60Test { + private static _60.Solution1 solution1; + + @BeforeClass + public static void setup() { + solution1 = new _60.Solution1(); + } + + @Test + public void test1() { + assertEquals("231", solution1.getPermutation(3, 4)); + } +}