Skip to content

Commit 1b8480b

Browse files
committed
feat: Add Abbreviation new algorithm with Junit tests
1 parent 213fd5a commit 1b8480b

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
public class Abbreviation {
14+
15+
/**
16+
* Determines if string `a` can be transformed into string `b` by capitalizing
17+
* some of its lowercase letters and deleting the rest.
18+
*
19+
* @param a The input string which may contain both uppercase and lowercase letters.
20+
* @param b The target string containing only uppercase letters.
21+
* @return {@code true} if string `a` can be transformed into string `b`,
22+
* {@code false} otherwise.
23+
*
24+
* Time Complexity: O(n * m) where n = length of string `a` and m = length of string `b`.
25+
* Space Complexity: O(n * m) due to the dynamic programming table.
26+
*/
27+
public static boolean abbr(String a, String b) {
28+
int n = a.length();
29+
int m = b.length();
30+
31+
boolean[][] dp = new boolean[n + 1][m + 1];
32+
33+
dp[0][0] = true;
34+
35+
for (int i = 0; i < n; i++) {
36+
for (int j = 0; j <= m; j++) {
37+
if (dp[i][j]) {
38+
// Case 1: If the current characters match (or can be capitalized to match)
39+
if (j < m && Character.toUpperCase(a.charAt(i)) == b.charAt(j)) {
40+
dp[i + 1][j + 1] = true;
41+
}
42+
// Case 2: If the character in `a` is lowercase, we can skip it
43+
if (Character.isLowerCase(a.charAt(i))) {
44+
dp[i + 1][j] = true;
45+
}
46+
}
47+
}
48+
}
49+
50+
return dp[n][m];
51+
}
52+
}
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", true),
22+
23+
// Test case where transformation is impossible
24+
Arguments.of("dBcd", "ABC", false),
25+
26+
// Test case with exact match (all uppercase)
27+
Arguments.of("ABC", "ABC", true),
28+
29+
// Test case where input string contains all required letters plus extra lowercase letters
30+
Arguments.of("aAbBcC", "ABC", true),
31+
32+
// Test case with only lowercase letters in input
33+
Arguments.of("abcd", "ABCD", true),
34+
35+
// Test case with an empty second string (b)
36+
Arguments.of("abc", "", true),
37+
38+
// Test case with an empty first string (a) but non-empty second string (b)
39+
Arguments.of("", "A", false),
40+
41+
// Complex case with interleaved letters
42+
Arguments.of("daBcAbCd", "ABCD", false));
43+
}
44+
}

0 commit comments

Comments
 (0)