1
- pub struct Solution { }
2
- impl Solution {
1
+ // Time: O(n) ~ O(n^2)
2
+ // Space: O(n)
3
+ // Counting Sort + Pruning + Bit Manipulation
4
+ // https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/maximum-product-of-word-lengths.cpp#L1-L40
5
+ pub struct Solution1 { }
6
+ impl Solution1 {
7
+ pub fn max_product ( words : Vec < String > ) -> i32 {
8
+ 0
9
+ }
10
+ }
11
+
12
+ // Time: O(nlogn) ~ O(n^2)
13
+ // Space: O(n)
14
+ // Sorting + Pruning + Bit Manipulation
15
+ pub struct Solution2 { }
16
+ impl Solution2 {
17
+ pub fn max_product ( words : Vec < String > ) -> i32 {
18
+ let mut sorted_words = words. clone ( ) ;
19
+ sorted_words. sort_by ( |a, b| b. len ( ) . cmp ( & a. len ( ) ) ) ;
20
+ let mut bits: Vec < i32 > = vec ! [ 0 ; words. len( ) ] ;
21
+ for i in 0 ..sorted_words. len ( ) {
22
+ for c in sorted_words[ i] . chars ( ) {
23
+ bits[ i] |= 1 << ( c as u32 - 'a' as u32 ) ;
24
+ }
25
+ }
26
+ let mut max_product: i32 = 0 ;
27
+ let mut i: usize = 0 ;
28
+ while i + 1 < sorted_words. len ( ) && sorted_words[ i] . len ( ) . pow ( 2 ) as i32 > max_product {
29
+ let mut j: usize = i + 1 ;
30
+ while j < sorted_words. len ( )
31
+ && ( sorted_words[ i] . len ( ) * sorted_words[ j] . len ( ) ) as i32 > max_product
32
+ {
33
+ if bits[ i] & bits[ j] == 0 {
34
+ max_product = ( sorted_words[ i] . len ( ) * sorted_words[ j] . len ( ) ) as i32 ;
35
+ }
36
+ j += 1 ;
37
+ }
38
+ i += 1 ;
39
+ }
40
+ max_product
41
+ }
42
+ }
43
+
44
+
45
+ // # Time: O(n) ~ O(n^2)
46
+ // # Space: O(n)
47
+ // https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/maximum-product-of-word-lengths.py#L1-L36
48
+ pub struct Solution3 { }
49
+ impl Solution3 {
50
+ pub fn max_product ( words : Vec < String > ) -> i32 {
51
+ 0
52
+ }
53
+ }
54
+
55
+ // # Time: O(nlogn) ~ O(n^2)
56
+ // # Space: O(n)
57
+ // # Sorting + Pruning + Bit Manipulation
58
+ // https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/maximum-product-of-word-lengths.py#L38-L62
59
+ pub struct Solution4 { }
60
+ impl Solution4 {
3
61
pub fn max_product ( words : Vec < String > ) -> i32 {
4
62
0
5
63
}
@@ -11,8 +69,32 @@ mod tests {
11
69
12
70
#[ test]
13
71
fn test_max_product ( ) {
14
- // assert_max_product!(Solution::max_product(vec!["abcw","baz","foo","bar","xtfn","abcdef"]), 16);
15
- // assert_max_product!(Solution::max_product(vec!["a","ab","abc","d","cd","bcd","abcd"]), 4);
16
- // assert_max_product!(Solution::max_product(vec!["a","aa","aaa","aaaa"]), 0);
72
+ assert_eq ! (
73
+ Solution2 :: max_product(
74
+ vec![ "abcw" , "baz" , "foo" , "bar" , "xtfn" , "abcdef" ]
75
+ . iter( )
76
+ . map( |& x| String :: from( x) )
77
+ . collect( )
78
+ ) ,
79
+ 16
80
+ ) ;
81
+ assert_eq ! (
82
+ Solution2 :: max_product(
83
+ vec![ "a" , "ab" , "abc" , "d" , "cd" , "bcd" , "abcd" ]
84
+ . iter( )
85
+ . map( |& x| String :: from( x) )
86
+ . collect( )
87
+ ) ,
88
+ 4
89
+ ) ;
90
+ assert_eq ! (
91
+ Solution2 :: max_product(
92
+ vec![ "a" , "aa" , "aaa" , "aaaa" ]
93
+ . iter( )
94
+ . map( |& x| String :: from( x) )
95
+ . collect( )
96
+ ) ,
97
+ 0
98
+ ) ;
17
99
}
18
- }
100
+ }
0 commit comments