Skip to content

Commit 2a518e3

Browse files
authored
Add Abbreviation algorithm (TheAlgorithms#5790)
1 parent b35f98a commit 2a518e3

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@
261261
* [StrassenMatrixMultiplication](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java)
262262
* [TilingProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/TilingProblem.java)
263263
* dynamicprogramming
264+
* [Abbreviation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/Abbreviation.java)
264265
* [BoardPath](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoardPath.java)
265266
* [BoundaryFill](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoundaryFill.java)
266267
* [BruteForceKnapsack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BruteForceKnapsack.java)
@@ -862,6 +863,7 @@
862863
* [StrassenMatrixMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java)
863864
* [TilingProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/TilingProblemTest.java)
864865
* dynamicprogramming
866+
* [AbbreviationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/AbbreviationTest.java)
865867
* [BoardPathTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoardPathTest.java)
866868
* [BoundaryFillTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoundaryFillTest.java)
867869
* [BruteForceKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BruteForceKnapsackTest.java)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
/**
4+
* A class that provides a solution to the abbreviation problem.
5+
*
6+
* Problem: Given two strings, `a` and `b`, determine if string `a` can be
7+
* transformed into string `b` by performing the following operations:
8+
* 1. Capitalize zero or more of `a`'s lowercase letters (i.e., convert them to uppercase).
9+
* 2. Delete any of the remaining lowercase letters from `a`.
10+
*
11+
* The task is to determine whether it is possible to make string `a` equal to string `b`.
12+
*
13+
* @author Hardvan
14+
*/
15+
public final class Abbreviation {
16+
private Abbreviation() {
17+
}
18+
19+
/**
20+
* Determines if string `a` can be transformed into string `b` by capitalizing
21+
* some of its lowercase letters and deleting the rest.
22+
*
23+
* @param a The input string which may contain both uppercase and lowercase letters.
24+
* @param b The target string containing only uppercase letters.
25+
* @return {@code true} if string `a` can be transformed into string `b`,
26+
* {@code false} otherwise.
27+
*
28+
* Time Complexity: O(n * m) where n = length of string `a` and m = length of string `b`.
29+
* Space Complexity: O(n * m) due to the dynamic programming table.
30+
*/
31+
public static boolean abbr(String a, String b) {
32+
int n = a.length();
33+
int m = b.length();
34+
35+
boolean[][] dp = new boolean[n + 1][m + 1];
36+
37+
dp[0][0] = true;
38+
39+
for (int i = 0; i < n; i++) {
40+
for (int j = 0; j <= m; j++) {
41+
if (dp[i][j]) {
42+
// Case 1: If the current characters match (or can be capitalized to match)
43+
if (j < m && Character.toUpperCase(a.charAt(i)) == b.charAt(j)) {
44+
dp[i + 1][j + 1] = true;
45+
}
46+
// Case 2: If the character in `a` is lowercase, we can skip it
47+
if (Character.isLowerCase(a.charAt(i))) {
48+
dp[i + 1][j] = true;
49+
}
50+
}
51+
}
52+
}
53+
54+
return dp[n][m];
55+
}
56+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public class AbbreviationTest {
11+
12+
@ParameterizedTest
13+
@MethodSource("provideTestCases")
14+
public void testAbbreviation(String a, String b, boolean expected) {
15+
assertEquals(expected, Abbreviation.abbr(a, b));
16+
}
17+
18+
private static Stream<Arguments> provideTestCases() {
19+
return Stream.of(
20+
// Example test case from problem description
21+
Arguments.of("daBcd", "ABC", Boolean.TRUE),
22+
23+
// Test case where transformation is impossible
24+
Arguments.of("dBcd", "ABC", Boolean.FALSE),
25+
26+
// Test case with exact match (all uppercase)
27+
Arguments.of("ABC", "ABC", Boolean.TRUE),
28+
29+
// Test case where input string contains all required letters plus extra lowercase letters
30+
Arguments.of("aAbBcC", "ABC", Boolean.TRUE),
31+
32+
// Test case with only lowercase letters in input
33+
Arguments.of("abcd", "ABCD", Boolean.TRUE),
34+
35+
// Test case with an empty second string (b)
36+
Arguments.of("abc", "", Boolean.TRUE),
37+
38+
// Test case with an empty first string (a) but non-empty second string (b)
39+
Arguments.of("", "A", Boolean.FALSE),
40+
41+
// Complex case with interleaved letters
42+
Arguments.of("daBcAbCd", "ABCD", Boolean.FALSE));
43+
}
44+
}

0 commit comments

Comments
 (0)