AdSense

Monday, October 31, 2016

Find All Anagrams in a String

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
The order of output does not matter.
Example 1:
Input:
s: "cbaebabacd" p: "abc"

Output:
[0, 6]

Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".
Example 2:
Input:
s: "abab" p: "ab"

Output:
[0, 1, 2]

Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".


This is a quite easy problem, except... it's easy to exceed time limit. One way to optimize is to calculate the first substring in s with length of p, i.e., s.substring(0, p.length()), check if they are anagrams, if they are, add 0 to result. Then we go with a sliding window manner, we add one char to the substring, and remove one char from it. After this operation, we compare if the substrings are anagrams.

public List<integer> findAnagrams(String s, String p) {
        List<integer> rst = new ArrayList<>();
        int lenS = s.length();
        int lenP = p.length();
        if (lenS == 0 || lenP == 0 || lenS < lenP) {
            return rst;
        }
        Map<character, integer> pCounter = new HashMap<>();
        Map<character, integer> sCounter = new HashMap<>(pCounter);
        for (int i = 0; i < lenP; i++) {
            char c1 = p.charAt(i);
            char c2 = s.charAt(i);
            pCounter.put(c1, pCounter.containsKey(c1) ? pCounter.get(c1) + 1 : 1);
            sCounter.put(c2, sCounter.containsKey(c2) ? sCounter.get(c2) + 1 : 1);
        }

        if (pCounter.equals(sCounter)) {
            rst.add(0);
        }

        for (int i = lenP; i < lenS; i++) {
            char c2 = s.charAt(i - lenP);
            if (sCounter.get(c2) == 1) {
                sCounter.remove(c2);
            } else {
                sCounter.put(c2, sCounter.get(c2) - 1);
            }
            char c1 = s.charAt(i);
            sCounter.put(c1, sCounter.containsKey(c1) ? sCounter.get(c1) + 1 : 1);
            if (pCounter.containsKey(c1) && pCounter.equals(sCounter)) {
                rst.add(i - lenP + 1);
            }
        }
        return rst;
    }


1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete