Showing posts with label Leetcode 76: Minimum Windows Substring. Show all posts
Showing posts with label Leetcode 76: Minimum Windows Substring. Show all posts

Tuesday, March 6, 2018

Find smallest substring containing all keys

March 6, 2018

Introduction


It was so exciting to know that the peer got the algorithm when I booked the interview early this afternoon. I know that the algorithm is hard level, and definitely it is very interesting to learn how the peer will solve the algorithm, specially if the peer does not work on the algorithm before.


Code practice


I know that the hard level algorithm is biased for the people never working on before. So it turned out that the peer came out the linear time solution with optimal time complexity, but the design has flaw which could not find smallest substring.

This is the first time I evaluated the idea as the interviewer, and then I came out the counter example to explain the defect in the design on line 35.

What I saw is that the peer was very calm and approached the problem, and then wrote code ready to run the test cases.

Here is the code I reviewed. I also learn some technology and know more about react framework. I did spend 30 minutes on react course before the mock interview today.


Friday, June 30, 2017

Leetcode 76: Minimum Window Substring

June 30, 2017

Introduction



Leetcode 76 minimum windows substring is such a popular algorithm, Julia was asked to work on the algorithm in mocking experience, the peer likes Julia to work on the string algorithm, and the peer gave the advice that Julia's solution will have time out issue.

Algorithm study 



Mocking 8:00 pm - 8:30 pm

Here is the C# practice in mocking experience, the code has timeout issue. Time complexity should be cut down.

Here is the C# practice to pass online judge of Leetcode 76.

Leetcode Discussion


Read one of discussions - link is here.

Need to write a short and better solution.

The current window is s[i:j] and the result window is s[I:J]. In need[c] I store how many times I need character c (can be negative) and missing tells how many characters are still missing. In the loop, first add the new character to the window. Then, if nothing is missing, remove as much as possible from the window start and then update the result.

Julia is very good at writing the code, she just need a good idea to work on.

C# code is rewritten, link is here.

What can I say after I debug the code using a test case? This code is hard to understand, one dictionary serves multiple purposes. It reminds me the algebra.

For example, "xxyz", search string is "xyz".
We can keep the search string in the dictionary<char, int>
dict['x'] = 1,
dict['y'] = 1,
dict['z'] = 1

Now, we like to use those values to track how many in the sliding window. If the sliding window is "xx", then dict['x'] = -1. If the value is 0, then the number of char is exactly what need. -1 means that there is extra one.

We also keep count of how many chars we need in search string. First visit of x in sliding window "xx" we increment count variable once, but second one we do not do that. In other words, there is only one 'x' in search string. From any iteration, it is easy to tell if the sliding window contains the search string by comparing count variable with the length of search string.

Queue is not necessary, just track the left position of sliding window. One advantage to use queue is to filter out characters not in the search string.

Move Queue from the code, and come back to work on the algorithm later.

Most important is to check time complexity of this idea, it is better than O(s.Length * t.Length), actually it is O(s.Length).

Sunday, June 18, 2017

Leetcode 76: Minimum Windows Substring

June 18, 2017

Problem Statement

First step Julia spent over 60 minutes to write and then failed several times, she finally passed first 267 test cases, and last test case failed with timeout error. C# code is here.

A lot of failed test case in her practice:

1. A lot of out-of-boundary index error, "abc", "a"
line 57 and 58, search[left] - make sure that left is in the boundary of the array

2. "a", "aa", need to check counts as well.