Wednesday, July 18, 2018

Leetcode 438. Find All Anagrams in a String

July 18, 2018

Introduction



It is easy level algorithm related to find anagrams in sliding window. The problem can be solved using counting sort and dynamic programming. It is so nice experience to write a C# solution using less than 20 minutes.


My practice



I quickly fixed the issue since online judge reported the errors.

Here are the highlights of issues found:

1. Line 42, need to add extra checking in the second condition: (pLength + i - 1) < sLength
2. Line 45, missing - 'a' to convert to integer
3. Make sure on Line45 string s access of index is in the range, so (pLength + i - 1) < sLength is added. Think about i = 1, end position is pLength - 1, and then think about start position from i and end position pLength + i - 1. (i, pLength + i - 1) covers pLength's chars.

Here is c# code:


No comments:

Post a Comment