C# Solutioin using bit operation


  • 0
    X
    public class Solution {
        public int MaxProduct(string[] words) {
            
            if (words == null || words.Length <=1)
            {
                return 0;
            }
            
            Dictionary<string, int> wordDict = new Dictionary<string, int>();
            
            for(int i=0; i< words.Length; i++)
            {
                wordDict[words[i]] = GetBinary(words[i]);
            }
            
            int maxProd = 0;
            
            foreach (var pairA in wordDict)
            {
                foreach(var pairB in wordDict)
                {
                    if ((pairA.Key != pairB.Key) && ((pairA.Value & pairB.Value) ==0))
                    {
                        int currProd = pairA.Key.Length * pairB.Key.Length;
                        
                        maxProd = currProd > maxProd ? currProd : maxProd;
                    }
                }
            }
            
            return maxProd;
        }
        
        static int GetBinary(string a)
        {
            int result = 0;
            
            for (int i=0; i< a.Length; i++)
            {
                result = result | 1<< (a[i] - 'a');
            }
            
            return result;
        }
    }
    

Log in to reply
 

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