Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ members = [
"find_and_replace_pattern",
"to_lower_case",
"evaluate_reverse_polish_notation",
"partitioning_into_minimum_number_of_deci_binary_numbers",
"maximum_product_of_word_lengths",
]
9 changes: 9 additions & 0 deletions maximum_product_of_word_lengths/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "maximum_product_of_word_lengths"
version = "0.1.0"
authors = ["Ryan Li <conbas2019@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
96 changes: 96 additions & 0 deletions maximum_product_of_word_lengths/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
pub struct Solution {}

impl Solution {
fn share_common_letters(
mask_cache: &mut Vec<i32>,
i1: usize,
w1: &String,
i2: usize,
w2: &String,
) -> bool {
let mask1 = match mask_cache.get(i1) {
Some(&x) if x != 0 => x,
_ => {
let mask = w1
.chars()
.fold(0, |acc, x| acc | 1 << (x as u8 - 'a' as u8));
mask_cache[i1] = mask;
mask
}
};
let mask2 = match mask_cache.get(i2) {
Some(&x) if x != 0 => x,
_ => {
let mask = w2
.chars()
.fold(0, |acc, x| acc | 1 << (x as u8 - 'a' as u8));
mask_cache[i2] = mask;
mask
}
};
mask1 & mask2 != 0
}

fn get_length(length_cache: &mut Vec<usize>, index: usize, word: &String) -> usize {
match length_cache.get(index) {
Some(&l) if l != 0 => l,
_ => {
let length = word.chars().count();
length_cache[index] = length;
length
}
}
}

pub fn max_product(words: Vec<String>) -> i32 {
let mut length_cache = vec![0; words.len()];
let mut mask_cache = vec![0; words.len()];
let mut max = 0;
for (i, w1) in words.iter().enumerate() {
for j in i + 1..words.len() {
let w2 = &words[j];
if Self::share_common_letters(&mut mask_cache, i, w1, j, w2) {
continue;
}
max = max.max(
(Self::get_length(&mut length_cache, i, w1)
* Self::get_length(&mut length_cache, j, w2)) as i32,
);
}
}
max
}
}

#[cfg(test)]
mod tests {
use super::*;

fn it_works(words: Vec<&str>, expected: i32) {
assert_eq!(
Solution::max_product(words.iter().map(|x| x.to_string()).collect()),
expected
);
}

#[test]
fn example_1() {
let words = vec!["abcw", "baz", "foo", "bar", "xtfn", "abcdef"];
let expected = 16;
it_works(words, expected);
}

#[test]
fn example_2() {
let words = vec!["a", "ab", "abc", "d", "cd", "bcd", "abcd"];
let expected = 4;
it_works(words, expected);
}

#[test]
fn example_3() {
let words = vec!["a", "aa", "aaa", "aaaa"];
let expected = 0;
it_works(words, expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "partitioning_into_minimum_number_of_deci_binary_numbers"
version = "0.1.0"
authors = ["Ryan Li <conbas2019@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
31 changes: 31 additions & 0 deletions partitioning_into_minimum_number_of_deci_binary_numbers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
pub struct Solution {}

impl Solution {
pub fn min_partitions(n: String) -> i32 {
n.chars().max().unwrap() as i32 - '0' as i32
}
}

#[cfg(test)]
mod tests {
use super::*;

fn it_works(n: &str, expected: i32) {
assert_eq!(Solution::min_partitions(n.to_string()), expected);
}

#[test]
fn example_1() {
it_works("32", 3);
}

#[test]
fn example_2() {
it_works("82734", 8);
}

#[test]
fn example_3() {
it_works("27346209830709182346", 9);
}
}