Skip to content

Commit f3b79f4

Browse files
Update abbreviation.rs
1 parent 09e043b commit f3b79f4

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/dynamic_programming/abbreviation.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Abbreviation Problem Solution
22
//!
3-
//! This module solves the abbreviation problem: determining if string `a` can be
4-
//! transformed into string `b` by capitalizing zero or more lowercase letters and
3+
//! This module solves the abbreviation problem: determining if string `a` can be
4+
//! transformed into string `b` by capitalizing zero or more lowercase letters and
55
//! deleting all remaining lowercase letters.
66
77
/// Determines if string `a` can be transformed into string `b` by:
88
/// 1. Capitalizing zero or more lowercase letters in `a`
99
/// 2. Deleting all remaining lowercase letters
1010
///
11-
/// The solution uses dynamic programming where `dp[i][j]` represents whether
11+
/// The solution uses dynamic programming where `dp[i][j]` represents whether
1212
/// the first `i` characters of `a` can form the first `j` characters of `b`.
1313
///
1414
/// # Arguments
@@ -25,7 +25,7 @@
2525
/// # Examples
2626
/// ```
2727
/// use the_algorithms_rust::dynamic_programming::abbreviation;
28-
///
28+
///
2929
/// assert_eq!(abbreviation("daBcd", "ABC"), true);
3030
/// assert_eq!(abbreviation("dBcd", "ABC"), false);
3131
/// ```
@@ -34,36 +34,36 @@ pub fn abbreviation(a: &str, b: &str) -> bool {
3434
let b_chars: Vec<char> = b.chars().collect();
3535
let n = a_chars.len();
3636
let m = b_chars.len();
37-
37+
3838
// dp[i][j] represents whether first i chars of a can form first j chars of b
3939
let mut dp = vec![vec![false; m + 1]; n + 1];
40-
40+
4141
// Base case: empty string a can form empty string b
4242
dp[0][0] = true;
43-
43+
4444
// Fill the first column: we can form empty b by deleting all lowercase letters
4545
for i in 0..n {
4646
if a_chars[i].is_lowercase() {
4747
dp[i + 1][0] = dp[i][0];
4848
}
4949
}
50-
50+
5151
for i in 0..n {
5252
for j in 0..=m {
5353
if dp[i][j] {
5454
// If we can match current position, check next characters
5555
if j < m && a_chars[i].to_ascii_uppercase() == b_chars[j] {
5656
dp[i + 1][j + 1] = true;
5757
}
58-
58+
5959
// If current character in a is lowercase, we can delete it
6060
if a_chars[i].is_lowercase() {
6161
dp[i + 1][j] = true;
6262
}
6363
}
6464
}
6565
}
66-
66+
6767
dp[n][m]
6868
}
6969

@@ -86,6 +86,8 @@ mod tests {
8686
// Original test cases from the problem
8787
test_daBcd_ABC: ("daBcd", "ABC") => true,
8888
test_dBcd_ABC: ("dBcd", "ABC") => false,
89+
90+
// Additional test cases
8991
test_AbcE_ABE: ("AbcE", "ABE") => true,
9092
test_AbcE_ABC: ("AbcE", "ABC") => false,
9193
test_abcde_ABCDE: ("abcde", "ABCDE") => true,
@@ -94,15 +96,15 @@ mod tests {
9496
test_ABCDE_ABCD: ("ABCDE", "ABCD") => false,
9597
test_aBcDe_ABCDE: ("aBcDe", "ABCDE") => true,
9698
test_aBcDe_ABCD: ("aBcDe", "ABCD") => true,
97-
99+
98100
// Edge test cases
99101
test_empty_both: ("", "") => true,
100102
test_empty_a: ("", "ABC") => false,
101103
test_empty_b: ("abc", "") => true,
102104
test_only_lowercase: ("abc", "ABC") => true,
103105
test_only_uppercase: ("ABC", "ABC") => true,
104106
test_mismatched_uppercase: ("ABD", "ABC") => false,
105-
107+
106108
// Complex cases from HackerRank
107109
test_complex_1: ("LLZOSYAMQRMBTZXTQMQcKGLR", "LLZOSYAMBTZXMQKLR") => false,
108110
test_complex_2: ("MGYXKOVSMAHKOLAZZKWXKS", "MGXKOVSAHKOLZKKDP") => false,

0 commit comments

Comments
 (0)