@@ -0,0 +1,2014 @@
+package problems;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0A%0Apublic class SubstringWithConcatenationOfAllWords %7B%0A    public List%3CInteger%3E findSubstring(String S, String%5B%5D L) %7B%0A        final List%3CInteger%3E out = new ArrayList%3CInteger%3E();%0A        if (S == null %7C%7C L == null %7C%7C S.length() == 0 %7C%7C L.length == 0)%0A            return out;%0A%0A        final int len = L%5B0%5D.length();%0A        final int N = L.length * len;%0A        final HashMap%3CString, Integer%3E map = new HashMap%3CString, Integer%3E();%0A        HashMap%3CString, Integer%3E mapcopy = null;%0A        for (final String st : L) %7B%0A            if (!map.containsKey(st))%0A                map.put(st, 1);%0A            else%0A                map.put(st, map.get(st) + 1);%0A        %7D%0A%0A        for (int i = 0; i %3C S.length() - N + 1; i++) %7B%0A            final String a = S.substring(i, i + len);%0A            if (!map.containsKey(a))%0A                continue;%0A            else %7B%0A                mapcopy = new HashMap%3CString, Integer%3E(map);%0A                remove(mapcopy, a);%0A            %7D%0A%0A            for (int j = 1; j %3C L.length; j++) %7B%0A                String t = S.substring(i + j * len, i + (j + 1) * len);%0A                if (!remove(mapcopy, t))%0A                    break;%0A            %7D%0A            if (mapcopy.size() == 0)%0A                out.add(i);%0A        %7D%0A        return out;%0A    %7D%0A    %0A    boolean remove(HashMap%3CString, Integer%3E map, String s) %7B%0A        if (map.containsKey(s)) %7B%0A            final int count = map.get(s);%0A            if (count %3E 1)%0A                map.put(s, count - 1);%0A            else%0A                map.remove(s);%0A            return true;%0A        %7D%0A        return false;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        SubstringWithConcatenationOfAllWords sw = new SubstringWithConcatenationOfAllWords();%0A        String S = %22lingmindraboofooowingdingbarrwingmonkeypoundcake%22;%0A        String%5B%5D L = %7B %22fooo%22, %22barr%22, %22wing%22, %22ding%22, %22wing%22 %7D;%0A        System.out.println(sw.findSubstring(S, L));%0A    %7D%0A%7D%0A

Add Substring with Concatenation of All Words