My solution with 11ms runtime


  • 0
    Y
     public List<List<String>> groupStrings(String[] strings) {
        if(strings == null) return null;
        List<List<String>> res = new ArrayList();
        List<String> set = new ArrayList();
        int i;
        for(i =0 ; i<strings.length; i++){
            set.add(strings[i]);
        }
        Collections.sort(set);
        while(!set.isEmpty()){
            Iterator<String> it = set.iterator();
            String start = it.next();
            ArrayList subList = new ArrayList();
            subList.add(start);
            set.remove(start);
            for(int j =0; j<26; j++){
                while(set.contains(start)){
                    set.remove(start);
                    subList.add(start);
                }
                start = shift(start);                
            } 
            // Collections.sort(subList);
            res.add(subList);
        }
        return res;
      
    }
    
    private String shift(String s){
        if(s == null || s.length() == 0) return s;
        char[] c = new char[s.length()];
        for(int i = 0 ; i<s.length(); i++){
            if(s.charAt(i) == 'z'){
                c[i] = 'a';
            }else{
                c[i] = (char)(s.charAt(i) + 1);                
            }
    
        }
        return new String(c);
    }

Log in to reply
 

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