TLE, any suggestions ?


  • 0
    H
    public class Solution {
        public Set<Character> convertToSet(String temp){
    		Set<Character> s1 = new HashSet<>();
    		for(Character c : temp.toCharArray()) s1.add(c);
    		return s1;
    	}
    	
    	public int maxProduct(String[] words) {
    		
    		int len = words.length;
    		if(len == 0 || words == null) return 0;
    		int maxProduct = Integer.MIN_VALUE;
    		Set<Character> s1, s2, intersection;
    
    		for(int i=0; i < len; i++){
    			s1 = convertToSet(words[i]);
    			intersection = new HashSet<>(s1);
    			for(int j=1; j < len; j++){
    				s2 = convertToSet(words[j]);
    				intersection.retainAll(s2);
    				if(intersection.size() == 0){
    					maxProduct = Math.max(maxProduct, words[i].length() * words[j].length());
    				}
    			    intersection = new HashSet<>(s1);
    			}
    		}
    		return maxProduct;
    	}
    }
    

    Getting TLE on this, on some huge test case. Any suggestions ?


Log in to reply
 

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