-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1638: Count Substrings That Differ by One Character
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/problem_1638_count_substrings_that_differ_by_one_character/iterative.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
// See <https://leetcode.com/problems/count-substrings-that-differ-by-one-character/discuss/917985/JavaC%2B%2BPython-Time-O(nm)-Space-O(1)>. | ||
|
||
impl Solution { | ||
fn helper(s: &[u8], t: &[u8], result: &mut i32) { | ||
let mut length_1 = 0; | ||
let mut length_2 = 0; | ||
|
||
for (left, right) in s.iter().zip(t) { | ||
length_2 += 1; | ||
|
||
if left != right { | ||
length_1 = length_2; | ||
length_2 = 0; | ||
} | ||
|
||
*result += length_1; | ||
} | ||
} | ||
|
||
pub fn count_substrings(s: String, t: String) -> i32 { | ||
let s = s.as_bytes(); | ||
let t = t.as_bytes(); | ||
let mut result = 0; | ||
|
||
for i in 0..t.len() { | ||
Self::helper(s, &t[i..], &mut result); | ||
} | ||
|
||
for i in 1..s.len() { | ||
Self::helper(&s[i..], t, &mut result); | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn count_substrings(s: String, t: String) -> i32 { | ||
Self::count_substrings(s, t) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/problem_1638_count_substrings_that_differ_by_one_character/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn count_substrings(s: String, t: String) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [(("aba", "baba"), 6), (("ab", "bb"), 3)]; | ||
|
||
for ((s, t), expected) in test_cases { | ||
assert_eq!(S::count_substrings(s.to_string(), t.to_string()), expected); | ||
} | ||
} | ||
} |