diff --git a/src/main/java/com/leetcode/arrays/ShortestWordDistanceIII.java b/src/main/java/com/leetcode/arrays/ShortestWordDistanceIII.java index 0d404633..39679608 100644 --- a/src/main/java/com/leetcode/arrays/ShortestWordDistanceIII.java +++ b/src/main/java/com/leetcode/arrays/ShortestWordDistanceIII.java @@ -39,22 +39,15 @@ public static int findShortestDistance(String[] words, String word1, String word int indexWord1 = -1; int indexWord2 = -1; int minDistance = Integer.MAX_VALUE; + boolean isSameWord = word1.equals(word2); for (int i = 0; i < words.length; i++) { - if (words[i].equals(word1)) { - // if both words are same and the first index is already set then do nothing - if (word1.equals(word2) && indexWord1 != -1) { - - } else { - indexWord1 = i; - } + // Update indexWord1 only if: words are different OR this is the first occurrence + if (words[i].equals(word1) && (!isSameWord || indexWord1 == -1)) { + indexWord1 = i; } - if (words[i].equals(word2)) { - // if both words are same and i is same as first index then it implies its the - // first occurrence, skip and continue look for the second occurrence - if (word1.equals(word2) && i == indexWord1) { - continue; - } + // Update indexWord2 only if: words are different OR this is not the first occurrence + if (words[i].equals(word2) && (!isSameWord || i != indexWord1)) { indexWord2 = i; } if (indexWord1 != -1 && indexWord2 != -1) { diff --git a/src/test/java/BuySellStocksTest.java b/src/test/java/BuySellStocksTest.java new file mode 100644 index 00000000..530efa4d --- /dev/null +++ b/src/test/java/BuySellStocksTest.java @@ -0,0 +1,46 @@ + + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BuySellStocksTest { + @Test + void testEmptyArray() { + int[] prices = {}; + int result = BuySellStocks.maxProfit(prices); + + assertEquals(0, result); + } + + @Test + void testUpdateBuyPrice() { + int[] prices = {8, 5, 10}; + int result = BuySellStocks.maxProfit(prices); + + assertEquals(5, result); + } + + @Test + void testUpdateProfit() { + int[] prices = {1, 5, 10}; + int result = BuySellStocks.maxProfit(prices); + + assertEquals(9, result); + } + + @Test + void testNoUpdateProfit() { + int[] prices = {1, 10, 5}; + int result = BuySellStocks.maxProfit(prices); + + assertEquals(9, result); + } + + @Test + void testMixedUpdates() { + int[] prices = {10, 2, 7, 3, 8, 4}; + int result = BuySellStocks.maxProfit(prices); + + assertEquals(6, result); + } +} diff --git a/src/test/java/ClosestBinarySearchTreeValueTest.java b/src/test/java/ClosestBinarySearchTreeValueTest.java new file mode 100644 index 00000000..5acb1828 --- /dev/null +++ b/src/test/java/ClosestBinarySearchTreeValueTest.java @@ -0,0 +1,70 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class ClosestBinarySearchTreeValueTest { + @Test + void testNullRoot() { + assertEquals(-1, ClosestBinarySearchTreeValue.closestValue(null, 10.0) ); + } + + @Test + void testSingleNode() { + TreeNode root = new TreeNode(10); + + assertEquals(10, ClosestBinarySearchTreeValue.closestValue(root, 5.0)); + } + + @Test + void testCloserToLeftChild() { + // 10 + // / + // 7 + TreeNode root = new TreeNode(10); + root.left = new TreeNode(7); + + assertEquals(7, ClosestBinarySearchTreeValue.closestValue(root, 8.0)); + } + + @Test + void testCloserToRightChild() { + // 10 + // \ + // 13 + TreeNode root = new TreeNode(10); + root.right = new TreeNode(13); + + assertEquals(13, ClosestBinarySearchTreeValue.closestValue(root, 12.0)); + } + + @Test + void testTieBreaking() { + // 10 + // \ + // 13 + TreeNode root = new TreeNode(10); + root.right = new TreeNode(13); + + assertEquals(10, ClosestBinarySearchTreeValue.closestValue(root, 11.0)); + } + + @Test + void testComplexTree() { + // Let's stick to a valid BST construction. + // 10 + // / \ + // 7 13 + // / \ / \ + // 5 9 13 20 + + TreeNode root = new TreeNode(10); + root.left = new TreeNode(7); + root.right = new TreeNode(13); + root.left.left = new TreeNode(5); + root.left.right = new TreeNode(9); + root.right.left = new TreeNode(13); + root.right.right = new TreeNode(20); + + assertEquals(7, ClosestBinarySearchTreeValue.closestValue(root, 8.0)); + assertEquals(9, ClosestBinarySearchTreeValue.closestValue(root, 8.1)); + } +} diff --git a/src/test/java/RansomNoteTest.java b/src/test/java/RansomNoteTest.java new file mode 100644 index 00000000..cb9fe20c --- /dev/null +++ b/src/test/java/RansomNoteTest.java @@ -0,0 +1,40 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class RansomNoteTest { + @Test + void testBothEmpty() { + boolean result = RansomNote.canConstruct("", ""); + assertTrue(result); + } + + @Test + void testEmptyMagazine() { + boolean result = RansomNote.canConstruct("a", ""); + assertFalse(result); + } + + @Test + void testEmptyRansomeNote() { + boolean result = RansomNote.canConstruct("", "abc"); + assertTrue(result); + } + + @Test + void testCharCountZero() { + boolean result = RansomNote.canConstruct("aa", "a"); + assertFalse(result); + } + + @Test + void testExtraUnusedChars() { + boolean result = RansomNote.canConstruct("ab", "abcdxyz"); + assertTrue(result); + } + + @Test + void testMissingOneChar() { + boolean result = RansomNote.canConstruct("abcdxyz", "bcdxyz"); + assertFalse(result); + } +} diff --git a/src/test/java/ShortestWordDistanceIIITest.java b/src/test/java/ShortestWordDistanceIIITest.java new file mode 100644 index 00000000..3b2935a7 --- /dev/null +++ b/src/test/java/ShortestWordDistanceIIITest.java @@ -0,0 +1,39 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class ShortestWordDistanceIIITest { + @Test + void testDifferentWordsAdjacent() { + String[] words = {"practice", "makes", "perfect", "coding", "makes"}; + + assertEquals(1, ShortestWordDistanceIII.findShortestDistance(words, "makes", "coding")); + } + + @Test + void testDifferentWordsNotAdjacent() { + String[] words = {"practice", "makes", "perfect", "coding", "makes"}; + + assertEquals(3, ShortestWordDistanceIII.findShortestDistance(words, "practice", "coding")); + } + + @Test + void testSameWords() { + String[] words = {"practice", "makes", "perfect", "coding", "makes"}; + + assertEquals(3, ShortestWordDistanceIII.findShortestDistance(words, "makes", "makes")); + } + + @Test + void testWord1BeforeWord2() { + String[] words = {"practice", "makes", "perfect", "coding", "makes"}; + + assertEquals(1, ShortestWordDistanceIII.findShortestDistance(words, "makes", "perfect")); + } + + @Test + void testWord2BeforeWord1() { + String[] words = {"practice", "makes", "perfect", "coding", "makes"}; + + assertEquals(1, ShortestWordDistanceIII.findShortestDistance(words, "perfect", "makes")); + } +}