-
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.
- Loading branch information
Showing
3 changed files
with
75 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
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,50 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::collections::HashSet; | ||
|
||
impl Solution { | ||
pub fn distinct_names(ideas: Vec<String>) -> i64 { | ||
let mut groups = [(); 26].map(|()| HashSet::<u64>::new()); | ||
|
||
for idea in ideas { | ||
let mut iter = idea.bytes(); | ||
let prefix = usize::from(iter.next().unwrap()); | ||
let suffix = iter.fold(0, |result, c| (result << 7) | u64::from(c)); | ||
|
||
groups[prefix - usize::from(b'a')].insert(suffix); | ||
} | ||
|
||
let mut result = 0; | ||
let mut iter = groups.iter(); | ||
|
||
while let Some(left) = iter.next() { | ||
if !left.is_empty() { | ||
iter.clone().for_each(|right| { | ||
let intersections = left.intersection(right).count(); | ||
|
||
result += (left.len() - intersections) * (right.len() - intersections); | ||
}); | ||
} | ||
} | ||
|
||
(result * 2) as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn distinct_names(ideas: Vec<String>) -> i64 { | ||
Self::distinct_names(ideas) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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,24 @@ | ||
pub mod group_by_prefix; | ||
|
||
pub trait Solution { | ||
fn distinct_names(ideas: Vec<String>) -> i64; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&["coffee", "donuts", "time", "toffee"] as &[_], 6), | ||
(&["lack", "back"], 0), | ||
]; | ||
|
||
for (ideas, expected) in test_cases { | ||
assert_eq!( | ||
S::distinct_names(ideas.iter().copied().map(str::to_string).collect()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |