Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
279a307
feat(strings): add StringRotation to check if one string is a rotatio…
konduri-lakshmi-prasanna Oct 12, 2025
ff9573a
fix: remove main method and add unit tests for StringRotation
konduri-lakshmi-prasanna Oct 12, 2025
193a8d7
test: add JUnit tests for StringRotation
konduri-lakshmi-prasanna Oct 12, 2025
3fc73b6
fix: move test file to correct directory and format with clang-format
konduri-lakshmi-prasanna Oct 12, 2025
08fa4a9
fix: correct package name for StringRotationTest
konduri-lakshmi-prasanna Oct 12, 2025
bfee1bb
fix: correct package and test structure for StringRotation
konduri-lakshmi-prasanna Oct 12, 2025
1a7084c
fix: resolve Checkstyle violations in StringRotation and test
konduri-lakshmi-prasanna Oct 12, 2025
329c1d9
fix: make StringRotation final to satisfy Checkstyle
konduri-lakshmi-prasanna Oct 12, 2025
71ce106
Add BinaryTreePaths algorithm with user input example
konduri-lakshmi-prasanna Oct 13, 2025
a36cca2
Merge branch 'master' into add-binary-tree-paths
konduri-lakshmi-prasanna Oct 13, 2025
fc45db5
Fix: remove main method and input for build compliance
konduri-lakshmi-prasanna Oct 13, 2025
840a1a9
Add: BinaryTreePathsTest.java for unit testing
konduri-lakshmi-prasanna Oct 13, 2025
3a9855b
Fix: avoid star import in BinaryTreePathsTest to satisfy Checkstyle
konduri-lakshmi-prasanna Oct 13, 2025
c8c7318
Fix: reorder imports to satisfy clang-format and Checkstyle
konduri-lakshmi-prasanna Oct 13, 2025
d927d00
Fix: reorder imports as per clang-format rules
konduri-lakshmi-prasanna Oct 13, 2025
148d851
Merge branch 'master' into add-binary-tree-paths
konduri-lakshmi-prasanna Oct 13, 2025
32c594f
Merge branch 'master' into add-binary-tree-paths
konduri-lakshmi-prasanna Oct 15, 2025
7ab9b1a
Merge branch 'master' into add-binary-tree-paths
konduri-lakshmi-prasanna Oct 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.thealgorithms.backtracking;

import java.util.ArrayList;
import java.util.List;

public class BinaryTreePaths {

public static class TreeNode {
int val;
TreeNode left;
TreeNode right;

TreeNode(int val) {
this.val = val;
}
}

public List<String> binaryTreePaths(TreeNode root) {
List<String> list = new ArrayList<>();
if (root == null) {
return list;
}
dfs(root, "", list);
return list;
}

private void dfs(TreeNode node, String path, List<String> list) {
if (node == null) {
return;
}

if (path.isEmpty()) {
path = Integer.toString(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);
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/thealgorithms/strings/StringRotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.thealgorithms.strings;

public final class StringRotation {

private StringRotation() {
throw new UnsupportedOperationException("Utility class");
}

/**
* 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) {
if (str1 == null || str2 == null) {
return false;
}

if (str1.length() != str2.length()) {
return false;
}

String concatenated = str1 + str1;
return concatenated.contains(str2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.thealgorithms.backtracking;

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
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<String> 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<String> result = solver.binaryTreePaths(root);

assertEquals(List.of("42"), result);
}

@Test
void testEmptyTree() {
BinaryTreePaths solver = new BinaryTreePaths();
List<String> result = solver.binaryTreePaths(null);

assertTrue(result.isEmpty());
}
}
30 changes: 30 additions & 0 deletions src/test/java/com/thealgorithms/strings/StringRotationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.thealgorithms.strings;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

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));
}
}