Repository: TheAlgorithms/Java
Pull request #5790 (2024-10-14 03:38:11)

feat: Add `Abbreviation` new algorithm with Junit tests
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5790?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 64.24%. Comparing base [(`213fd5a`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/213fd5a4931ba1cb74fed76ff97043ae34ff174b?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`8ddbc1f`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/8ddbc1fe0ab7cccae1f1a3e79e7d7f8d451ed0e3?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5790      +/-   ##
============================================
+ Coverage     64.21%   64.24%   +0.03%     
- Complexity     4216     4224       +8     
============================================
  Files           588      589       +1     
  Lines         16426    16438      +12     
  Branches       3170     3175       +5     
============================================
+ Hits          10548    10561      +13     
  Misses         5448     5448              
+ Partials        430      429       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5790?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 3d215855..64297571 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -261,6 +261,7 @@
             * [StrassenMatrixMultiplication](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java)
             * [TilingProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/TilingProblem.java)
           * dynamicprogramming
+            * [Abbreviation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/Abbreviation.java)
             * [BoardPath](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoardPath.java)
             * [BoundaryFill](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoundaryFill.java)
             * [BruteForceKnapsack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BruteForceKnapsack.java)
@@ -862,6 +863,7 @@
             * [StrassenMatrixMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java)
             * [TilingProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/TilingProblemTest.java)
           * dynamicprogramming
+            * [AbbreviationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/AbbreviationTest.java)
             * [BoardPathTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoardPathTest.java)
             * [BoundaryFillTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoundaryFillTest.java)
             * [BruteForceKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BruteForceKnapsackTest.java)
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Abbreviation.java b/src/main/java/com/thealgorithms/dynamicprogramming/Abbreviation.java
new file mode 100644
index 00000000..60c0fd0a
--- /dev/null
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/Abbreviation.java
@@ -0,0 +1,56 @@
+package com.thealgorithms.dynamicprogramming;
+
+/**
+ * A class that provides a solution to the abbreviation problem.
+ *
+ * Problem: Given two strings, `a` and `b`, determine if string `a` can be
+ * transformed into string `b` by performing the following operations:
+ * 1. Capitalize zero or more of `a`'s lowercase letters (i.e., convert them to uppercase).
+ * 2. Delete any of the remaining lowercase letters from `a`.
+ *
+ * The task is to determine whether it is possible to make string `a` equal to string `b`.
+ *
+ * @author Hardvan
+ */
+public final class Abbreviation {
+    private Abbreviation() {
+    }
+
+    /**
+     * Determines if string `a` can be transformed into string `b` by capitalizing
+     * some of its lowercase letters and deleting the rest.
+     *
+     * @param a The input string which may contain both uppercase and lowercase letters.
+     * @param b The target string containing only uppercase letters.
+     * @return {@code true} if string `a` can be transformed into string `b`,
+     *         {@code false} otherwise.
+     *
+     * Time Complexity: O(n * m) where n = length of string `a` and m = length of string `b`.
+     * Space Complexity: O(n * m) due to the dynamic programming table.
+     */
+    public static boolean abbr(String a, String b) {
+        int n = a.length();
+        int m = b.length();
+
+        boolean[][] dp = new boolean[n + 1][m + 1];
+
+        dp[0][0] = true;
+
+        for (int i = 0; i < n; i++) {
+            for (int j = 0; j <= m; j++) {
+                if (dp[i][j]) {
+                    // Case 1: If the current characters match (or can be capitalized to match)
+                    if (j < m && Character.toUpperCase(a.charAt(i)) == b.charAt(j)) {
+                        dp[i + 1][j + 1] = true;
+                    }
+                    // Case 2: If the character in `a` is lowercase, we can skip it
+                    if (Character.isLowerCase(a.charAt(i))) {
+                        dp[i + 1][j] = true;
+                    }
+                }
+            }
+        }
+
+        return dp[n][m];
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/AbbreviationTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/AbbreviationTest.java
new file mode 100644
index 00000000..4e36edbd
--- /dev/null
+++ b/src/test/java/com/thealgorithms/dynamicprogramming/AbbreviationTest.java
@@ -0,0 +1,44 @@
+package com.thealgorithms.dynamicprogramming;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.stream.Stream;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+public class AbbreviationTest {
+
+    @ParameterizedTest
+    @MethodSource("provideTestCases")
+    public void testAbbreviation(String a, String b, boolean expected) {
+        assertEquals(expected, Abbreviation.abbr(a, b));
+    }
+
+    private static Stream<Arguments> provideTestCases() {
+        return Stream.of(
+            // Example test case from problem description
+            Arguments.of("daBcd", "ABC", Boolean.TRUE),
+
+            // Test case where transformation is impossible
+            Arguments.of("dBcd", "ABC", Boolean.FALSE),
+
+            // Test case with exact match (all uppercase)
+            Arguments.of("ABC", "ABC", Boolean.TRUE),
+
+            // Test case where input string contains all required letters plus extra lowercase letters
+            Arguments.of("aAbBcC", "ABC", Boolean.TRUE),
+
+            // Test case with only lowercase letters in input
+            Arguments.of("abcd", "ABCD", Boolean.TRUE),
+
+            // Test case with an empty second string (b)
+            Arguments.of("abc", "", Boolean.TRUE),
+
+            // Test case with an empty first string (a) but non-empty second string (b)
+            Arguments.of("", "A", Boolean.FALSE),
+
+            // Complex case with interleaved letters
+            Arguments.of("daBcAbCd", "ABCD", Boolean.FALSE));
+    }
+}