A problem with Group Anagrams


  • 0
    F

    I am confused that why I ran the algorithm on my eclipse correctly but failed to pass leetcode with the testcase ["tea","","eat","","tea",""]。

    public class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
    boolean used[]=new boolean[strs.length];
    List<List<String>> list=new ArrayList<>();
    List<String> tempList=new ArrayList<>();
    List<String> nullList=new ArrayList<>();
    String temp="";
    boolean f=true;
    boolean flag=true;

        while(flag){
        	//"eat", "tea", "tan", "ate", "nat", "bat"
            for(int i=0;i<strs.length;i++){
            	f=true;
    
                if(used[i]==false){
                    if(tempList.isEmpty()){
                        tempList.add(strs[i]);
                        temp=strs[i];
                        used[i]=true;
                    }
                    else{
                    		if(strs[i]==""){
                    			used[i]=true;
                    			nullList.add(strs[i]);
                    			f=false;
                    			continue;
                    		}
                    		else{
                    			for(int j=0;j<strs[i].length();j++){
                            		
                            		if(temp.indexOf(strs[i].charAt(j))<0){
                            			f=false;
                            			continue;
                            		}
                    		}
                                
               
                    	}
                            
                    	if(f){
                    		tempList.add(strs[i]);
                    		used[i]=true;
                    	}
                        
                    }
                    
                    
                }
            }
            list.add(new ArrayList<>(tempList));
            tempList.clear();
            flag=false;
            for(int k=0;k<strs.length;k++)
                if(used[k]==false)
                    flag=true;
        }
        
        if(!nullList.isEmpty()){
        	list.add(new ArrayList<>(nullList));
        }
    
        return list;
    }
    

    }


Log in to reply
 

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