From 279a30775ca8f95c0bfea69a67fbc42f5771ddbf Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Sun, 12 Oct 2025 21:05:53 +0530 Subject: [PATCH 01/14] feat(strings): add StringRotation to check if one string is a rotation of another Signed-off-by: konduri-lakshmi-prasanna --- .../thealgorithms/strings/StringRotation.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/StringRotation.java diff --git a/src/main/java/com/thealgorithms/strings/StringRotation.java b/src/main/java/com/thealgorithms/strings/StringRotation.java new file mode 100644 index 000000000000..e57a11438da0 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/StringRotation.java @@ -0,0 +1,45 @@ +package com.thealgorithms.strings; + +import java.util.Scanner; + +public class StringRotation { + + /** + * Checks if str2 is a rotation of str1 + * @param str1 Original string + * @param str2 String to check for rotation + * @return true if str2 is a rotation of str1, false otherwise + */ + public static boolean isRotation(String str1, String str2) { + if (str1 == null || str2 == null) { + return false; + } + + if (str1.length() != str2.length()) { + return false; + } + + String concatenated = str1 + str1; + return concatenated.contains(str2); + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.print("Enter first string: "); + String str1 = scanner.nextLine(); + + System.out.print("Enter second string: "); + String str2 = scanner.nextLine(); + + boolean result = isRotation(str1, str2); + + if (result) { + System.out.println("\"" + str2 + "\" is a rotation of \"" + str1 + "\"."); + } else { + System.out.println("\"" + str2 + "\" is NOT a rotation of \"" + str1 + "\"."); + } + + scanner.close(); + } +} From ff9573aa9a472ca650b8005f0df6d7832aae6729 Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Sun, 12 Oct 2025 21:17:47 +0530 Subject: [PATCH 02/14] fix: remove main method and add unit tests for StringRotation --- .../thealgorithms/strings/StringRotation.java | 28 ++++--------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/StringRotation.java b/src/main/java/com/thealgorithms/strings/StringRotation.java index e57a11438da0..e385c2c13eb6 100644 --- a/src/main/java/com/thealgorithms/strings/StringRotation.java +++ b/src/main/java/com/thealgorithms/strings/StringRotation.java @@ -1,11 +1,13 @@ package com.thealgorithms.strings; -import java.util.Scanner; - +/** + * Checks if one string is a rotation of another string. + */ public class StringRotation { /** - * Checks if str2 is a rotation of str1 + * Checks if str2 is a rotation of str1. + * * @param str1 Original string * @param str2 String to check for rotation * @return true if str2 is a rotation of str1, false otherwise @@ -22,24 +24,4 @@ public static boolean isRotation(String str1, String str2) { String concatenated = str1 + str1; return concatenated.contains(str2); } - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - - System.out.print("Enter first string: "); - String str1 = scanner.nextLine(); - - System.out.print("Enter second string: "); - String str2 = scanner.nextLine(); - - boolean result = isRotation(str1, str2); - - if (result) { - System.out.println("\"" + str2 + "\" is a rotation of \"" + str1 + "\"."); - } else { - System.out.println("\"" + str2 + "\" is NOT a rotation of \"" + str1 + "\"."); - } - - scanner.close(); - } } From 193a8d736f5d332c778d66423e633b01541cf332 Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Sun, 12 Oct 2025 21:23:53 +0530 Subject: [PATCH 03/14] test: add JUnit tests for StringRotation --- .../strings/StringRotationTest.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/StringRotationTest.java diff --git a/src/main/java/com/thealgorithms/strings/StringRotationTest.java b/src/main/java/com/thealgorithms/strings/StringRotationTest.java new file mode 100644 index 000000000000..68525a981453 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/StringRotationTest.java @@ -0,0 +1,28 @@ +package com.thealgorithms.strings; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +public class StringRotationTest { + + @Test + void testValidRotation() { + assertTrue(StringRotation.isRotation("waterbottle", "erbottlewat")); + } + + @Test + void testInvalidRotation() { + assertFalse(StringRotation.isRotation("hello", "world")); + } + + @Test + void testDifferentLengths() { + assertFalse(StringRotation.isRotation("abc", "abcd")); + } + + @Test + void testNullInput() { + assertFalse(StringRotation.isRotation(null, "abc")); + assertFalse(StringRotation.isRotation("abc", null)); + } +} From 3fc73b60daffc233c7df4acffd853e55424d5386 Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Sun, 12 Oct 2025 21:58:56 +0530 Subject: [PATCH 04/14] fix: move test file to correct directory and format with clang-format --- .../java/com/thealgorithms/strings/StringRotationTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) rename src/{main => test}/java/com/thealgorithms/strings/StringRotationTest.java (94%) diff --git a/src/main/java/com/thealgorithms/strings/StringRotationTest.java b/src/test/java/com/thealgorithms/strings/StringRotationTest.java similarity index 94% rename from src/main/java/com/thealgorithms/strings/StringRotationTest.java rename to src/test/java/com/thealgorithms/strings/StringRotationTest.java index 68525a981453..625934f99bfc 100644 --- a/src/main/java/com/thealgorithms/strings/StringRotationTest.java +++ b/src/test/java/com/thealgorithms/strings/StringRotationTest.java @@ -1,6 +1,7 @@ -package com.thealgorithms.strings; +package com.thealgorithms; import static org.junit.jupiter.api.Assertions.*; + import org.junit.jupiter.api.Test; public class StringRotationTest { From 08fa4a92057483df557c5dc297a5ea663e3f9044 Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Sun, 12 Oct 2025 22:03:09 +0530 Subject: [PATCH 05/14] fix: correct package name for StringRotationTest --- src/test/java/com/thealgorithms/strings/StringRotationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/strings/StringRotationTest.java b/src/test/java/com/thealgorithms/strings/StringRotationTest.java index 625934f99bfc..8ffadeba6108 100644 --- a/src/test/java/com/thealgorithms/strings/StringRotationTest.java +++ b/src/test/java/com/thealgorithms/strings/StringRotationTest.java @@ -1,4 +1,4 @@ -package com.thealgorithms; +package com.thealgorithms.strings; import static org.junit.jupiter.api.Assertions.*; From bfee1bbe854d3fb184a3189a8f78e1cb91185c82 Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Sun, 12 Oct 2025 22:16:33 +0530 Subject: [PATCH 06/14] fix: correct package and test structure for StringRotation --- src/main/java/com/thealgorithms/strings/StringRotation.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/StringRotation.java b/src/main/java/com/thealgorithms/strings/StringRotation.java index e385c2c13eb6..74ddf39a3ea9 100644 --- a/src/main/java/com/thealgorithms/strings/StringRotation.java +++ b/src/main/java/com/thealgorithms/strings/StringRotation.java @@ -1,13 +1,9 @@ package com.thealgorithms.strings; -/** - * Checks if one string is a rotation of another string. - */ public class StringRotation { /** - * Checks if str2 is a rotation of str1. - * + * Checks if str2 is a rotation of str1 * @param str1 Original string * @param str2 String to check for rotation * @return true if str2 is a rotation of str1, false otherwise From 1a7084c0ef1cf58f4a69343f2c0459ac5b464464 Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Sun, 12 Oct 2025 22:24:41 +0530 Subject: [PATCH 07/14] fix: resolve Checkstyle violations in StringRotation and test --- src/main/java/com/thealgorithms/strings/StringRotation.java | 4 ++++ .../java/com/thealgorithms/strings/StringRotationTest.java | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/strings/StringRotation.java b/src/main/java/com/thealgorithms/strings/StringRotation.java index 74ddf39a3ea9..8f222d295a2d 100644 --- a/src/main/java/com/thealgorithms/strings/StringRotation.java +++ b/src/main/java/com/thealgorithms/strings/StringRotation.java @@ -2,6 +2,10 @@ public class StringRotation { + private StringRotation() { + throw new UnsupportedOperationException("Utility class"); + } + /** * Checks if str2 is a rotation of str1 * @param str1 Original string diff --git a/src/test/java/com/thealgorithms/strings/StringRotationTest.java b/src/test/java/com/thealgorithms/strings/StringRotationTest.java index 8ffadeba6108..b76df428277c 100644 --- a/src/test/java/com/thealgorithms/strings/StringRotationTest.java +++ b/src/test/java/com/thealgorithms/strings/StringRotationTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.strings; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; From 329c1d9ef0928974d134e7b3049e2b7c4a2b1b17 Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Sun, 12 Oct 2025 22:32:15 +0530 Subject: [PATCH 08/14] fix: make StringRotation final to satisfy Checkstyle --- .../java/com/thealgorithms/strings/StringRotation.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/StringRotation.java b/src/main/java/com/thealgorithms/strings/StringRotation.java index 8f222d295a2d..30a5725477a8 100644 --- a/src/main/java/com/thealgorithms/strings/StringRotation.java +++ b/src/main/java/com/thealgorithms/strings/StringRotation.java @@ -1,15 +1,16 @@ package com.thealgorithms.strings; -public class StringRotation { +public final class StringRotation { private StringRotation() { throw new UnsupportedOperationException("Utility class"); } /** - * Checks if str2 is a rotation of str1 - * @param str1 Original string - * @param str2 String to check for rotation + * Checks if str2 is a rotation of str1. + * + * @param str1 the original string + * @param str2 the string to check for rotation * @return true if str2 is a rotation of str1, false otherwise */ public static boolean isRotation(String str1, String str2) { From 71ce10608076b7132e27245f54d0d0fa0993f2eb Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Mon, 13 Oct 2025 22:18:08 +0530 Subject: [PATCH 09/14] Add BinaryTreePaths algorithm with user input example --- .../backtracking/BinaryTreePaths.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/main/java/com/thealgorithms/backtracking/BinaryTreePaths.java diff --git a/src/main/java/com/thealgorithms/backtracking/BinaryTreePaths.java b/src/main/java/com/thealgorithms/backtracking/BinaryTreePaths.java new file mode 100644 index 000000000000..ab0a26bb4d00 --- /dev/null +++ b/src/main/java/com/thealgorithms/backtracking/BinaryTreePaths.java @@ -0,0 +1,75 @@ +package com.thealgorithms.backtracking; +import java.util.*; +public class BinaryTreePaths { + static class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int val) { + this.val = val; + } + } + public List binaryTreePaths(TreeNode root) { + List list = new ArrayList<>(); + if (root == null) return list; + dfs(root, "", list); + return list; + } + + private void dfs(TreeNode node, String path, List list) { + if (node == null) return; + + if (path.isEmpty()) path = "" + node.val; + else path += "->" + node.val; + + if (node.left == null && node.right == null) { + list.add(path); + return; + } + + dfs(node.left, path, list); + dfs(node.right, path, list); + } + private static TreeNode buildTreeFromInput(Scanner sc) { + System.out.print("Enter number of nodes: "); + int n = sc.nextInt(); + if (n == 0) return null; + + System.out.println("Enter node values in level order (use -1 for nulls):"); + int[] values = new int[n]; + for (int i = 0; i < n; i++) values[i] = sc.nextInt(); + + if (values[0] == -1) return null; + TreeNode root = new TreeNode(values[0]); + Queue queue = new LinkedList<>(); + queue.add(root); + + int i = 1; + while (i < n && !queue.isEmpty()) { + TreeNode current = queue.poll(); + if (i < n && values[i] != -1) { + current.left = new TreeNode(values[i]); + queue.add(current.left); + } + i++; + if (i < n && values[i] != -1) { + current.right = new TreeNode(values[i]); + queue.add(current.right); + } + i++; + } + return root; + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + TreeNode root = buildTreeFromInput(sc); + + BinaryTreePaths solver = new BinaryTreePaths(); + List result = solver.binaryTreePaths(root); + + System.out.println("All root-to-leaf paths: " + result); + sc.close(); + } +} From fc45db5eab09c5cb99d6adae01986de68f2129e3 Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Mon, 13 Oct 2025 22:31:03 +0530 Subject: [PATCH 10/14] Fix: remove main method and input for build compliance --- .../backtracking/BinaryTreePaths.java | 65 +++++-------------- 1 file changed, 18 insertions(+), 47 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/BinaryTreePaths.java b/src/main/java/com/thealgorithms/backtracking/BinaryTreePaths.java index ab0a26bb4d00..df8bd25c3efe 100644 --- a/src/main/java/com/thealgorithms/backtracking/BinaryTreePaths.java +++ b/src/main/java/com/thealgorithms/backtracking/BinaryTreePaths.java @@ -1,7 +1,11 @@ package com.thealgorithms.backtracking; -import java.util.*; + +import java.util.ArrayList; +import java.util.List; + public class BinaryTreePaths { - static class TreeNode { + + public static class TreeNode { int val; TreeNode left; TreeNode right; @@ -10,18 +14,26 @@ static class TreeNode { this.val = val; } } + public List binaryTreePaths(TreeNode root) { List list = new ArrayList<>(); - if (root == null) return list; + if (root == null) { + return list; + } dfs(root, "", list); return list; } private void dfs(TreeNode node, String path, List list) { - if (node == null) return; + if (node == null) { + return; + } - if (path.isEmpty()) path = "" + node.val; - else path += "->" + node.val; + if (path.isEmpty()) { + path = Integer.toString(node.val); + } else { + path += "->" + node.val; + } if (node.left == null && node.right == null) { list.add(path); @@ -31,45 +43,4 @@ private void dfs(TreeNode node, String path, List list) { dfs(node.left, path, list); dfs(node.right, path, list); } - private static TreeNode buildTreeFromInput(Scanner sc) { - System.out.print("Enter number of nodes: "); - int n = sc.nextInt(); - if (n == 0) return null; - - System.out.println("Enter node values in level order (use -1 for nulls):"); - int[] values = new int[n]; - for (int i = 0; i < n; i++) values[i] = sc.nextInt(); - - if (values[0] == -1) return null; - TreeNode root = new TreeNode(values[0]); - Queue queue = new LinkedList<>(); - queue.add(root); - - int i = 1; - while (i < n && !queue.isEmpty()) { - TreeNode current = queue.poll(); - if (i < n && values[i] != -1) { - current.left = new TreeNode(values[i]); - queue.add(current.left); - } - i++; - if (i < n && values[i] != -1) { - current.right = new TreeNode(values[i]); - queue.add(current.right); - } - i++; - } - return root; - } - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - TreeNode root = buildTreeFromInput(sc); - - BinaryTreePaths solver = new BinaryTreePaths(); - List result = solver.binaryTreePaths(root); - - System.out.println("All root-to-leaf paths: " + result); - sc.close(); - } } From 840a1a910b3398f38afeedcfa2d5f38494598fb6 Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Mon, 13 Oct 2025 22:32:51 +0530 Subject: [PATCH 11/14] Add: BinaryTreePathsTest.java for unit testing --- .../backtracking/BinaryTreePathsTest.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java diff --git a/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java b/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java new file mode 100644 index 000000000000..9dcad502000e --- /dev/null +++ b/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java @@ -0,0 +1,41 @@ +package com.thealgorithms.backtracking; + +import org.junit.jupiter.api.Test; +import java.util.List; +import static org.junit.jupiter.api.Assertions.*; + +public class BinaryTreePathsTest { + + @Test + void testBinaryTreePathsBasic() { + BinaryTreePaths.TreeNode root = new BinaryTreePaths.TreeNode(1); + root.left = new BinaryTreePaths.TreeNode(2); + root.right = new BinaryTreePaths.TreeNode(3); + root.left.right = new BinaryTreePaths.TreeNode(5); + + BinaryTreePaths solver = new BinaryTreePaths(); + List result = solver.binaryTreePaths(root); + + assertEquals(2, result.size()); + assertTrue(result.contains("1->2->5")); + assertTrue(result.contains("1->3")); + } + + @Test + void testSingleNodeTree() { + BinaryTreePaths.TreeNode root = new BinaryTreePaths.TreeNode(42); + + BinaryTreePaths solver = new BinaryTreePaths(); + List result = solver.binaryTreePaths(root); + + assertEquals(List.of("42"), result); + } + + @Test + void testEmptyTree() { + BinaryTreePaths solver = new BinaryTreePaths(); + List result = solver.binaryTreePaths(null); + + assertTrue(result.isEmpty()); + } +} From 3a9855b1dd255e2efd7d6616dcc1b90a2c463bab Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Mon, 13 Oct 2025 23:36:25 +0530 Subject: [PATCH 12/14] Fix: avoid star import in BinaryTreePathsTest to satisfy Checkstyle --- .../com/thealgorithms/backtracking/BinaryTreePathsTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java b/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java index 9dcad502000e..b7b2e4bd2fb4 100644 --- a/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java +++ b/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java @@ -2,7 +2,8 @@ import org.junit.jupiter.api.Test; import java.util.List; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class BinaryTreePathsTest { From c8c7318fd4d3b29b294bbce27be59bab30e2389d Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Mon, 13 Oct 2025 23:47:59 +0530 Subject: [PATCH 13/14] Fix: reorder imports to satisfy clang-format and Checkstyle --- .../java/com/thealgorithms/backtracking/BinaryTreePathsTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java b/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java index b7b2e4bd2fb4..78ebcc2166cc 100644 --- a/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java +++ b/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java @@ -2,6 +2,7 @@ import org.junit.jupiter.api.Test; import java.util.List; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; From d927d0024fd9dacd14c70011d4428ed0ab2012a3 Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Mon, 13 Oct 2025 23:55:47 +0530 Subject: [PATCH 14/14] Fix: reorder imports as per clang-format rules --- .../com/thealgorithms/backtracking/BinaryTreePathsTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java b/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java index 78ebcc2166cc..7367a1d7751d 100644 --- a/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java +++ b/src/test/java/com/thealgorithms/backtracking/BinaryTreePathsTest.java @@ -1,11 +1,11 @@ package com.thealgorithms.backtracking; -import org.junit.jupiter.api.Test; -import java.util.List; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.List; +import org.junit.jupiter.api.Test; + public class BinaryTreePathsTest { @Test