O(n^2) solution with bit manipulation


  • 0
    A
    public class Solution {
    public int MaxProduct(string[] words) {
        if (!words.Any()) 
        {
            return 0;
        }
        
        var dictionary = words.Aggregate(
        new Dictionary<String, int>(),
        (acc, x) => {
           acc[x] = x.Aggregate(
               0,
               (agg, y) => {
                    return agg | (1 << ('a' - y));
               });
    
           return acc;
        });
    
        var empty = new KeyValuePair<String, int>(String.Empty, 0);
        return dictionary
            .SelectMany(
                x => dictionary
                        .Where(y => (y.Value & x.Value) == 0)
                        .DefaultIfEmpty(empty)
                        .Select(y => y.Key.Length * x.Key.Length)).Max();
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.