From 279a30775ca8f95c0bfea69a67fbc42f5771ddbf Mon Sep 17 00:00:00 2001 From: konduri-lakshmi-prasanna Date: Sun, 12 Oct 2025 21:05:53 +0530 Subject: [PATCH 1/8] 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 2/8] 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 3/8] 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 4/8] 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 5/8] 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 6/8] 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 7/8] 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 8/8] 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) {