Missed the window to submit but will it work?


  • 0
    A

    Hi, I missed the window to submit solution. For example inputs, it worked fine. Anything wrong anybody sees in the solution? Is there an easier way?
    """

    public Set<String> purmutation(String p) {
    	Set<String> sets = new HashSet<>();
    	purmutation("", p, sets);
    	return sets;
    }
    
    public void purmutation(String prefix, String str, Set<String> sets) {
    	int n = str.length();
    	if (n == 1) {
    		sets.add(prefix + str);
    	} else {
    		for (int i = 0; i < n; i++) {
    			purmutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n), sets);
    		}
    
    	}
    }
    
    public List<Integer> findAnagrams(String s, String p) {
    	List<Integer> list = new ArrayList<>();
    	Set<String> sets = purmutation(p);
    	for (String str : sets) {
    		int ind = s.indexOf(str);
    		if (ind != -1) {
    			list.add(ind);
    			int i = ind + str.length();
    			while (i < s.length()) {
    				ind = s.substring(i).indexOf(str);
    				if (ind != -1) {
    					list.add(i + ind);
    					i += str.length();
    				} else {
    					break;
    				}
    			}
    		}
    	}
    	return list;
    }
    

    """


  • 0

    @agrawalm You can try submit now, previously it returned an error but it should work now.


Log in to reply
 

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