C# beat 100% 159ms


  • 0
    S
    public class Solution {
        public int MaxProduct(string[] words) {
            int[] wordMasks = new int[words.Length];
            
            for (int i = 0; i < words.Length; i++){
                wordMasks[i] = SetBits(words[i]);
            }
            
            int product = 0;
            
            for (int i = 0; i < words.Length; i++){
                for (int j = i + 1; j < words.Length; j++){
                    if ((wordMasks[i] & wordMasks[j]) == 0){
                        product = Math.Max(product, words[i].Length * words[j].Length);
                    }
                }
            }
            
            return product;
        }
        
        private int SetBits(string str){
            int number = 0;
            foreach(char c in str){
                int index = c - 'a';
                int mask = (int)1 << index;
                number = number | mask;
            }
            
            return number;
        }
    }
    

Log in to reply
 

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