Sunday, October 20, 2019

1234. Replace the Substring for Balanced String - after the contest

Here is the post I learned to write in C#.

C# Code study: a solution can be written in two loops

I studied the code written by top player Lee215. I had to write my own C# code, and then asked myself questions, how do I understand to track sliding window count?

I chose to study top voted solution which is converted to a different problem. I like to describe this way, it is to find all sliding windows containing above average number of chars in "QWER". The dictionary to store is whole string except current sliding window. Why? It is so simple to write to the code and it can be solved in less than 15 minutes. The solution link is here.
Here are shortcuts I learn from your code:
  1. Define map using int as key, each char of "QWER" is the key which is ascii value; Avoid string IndexOf etc.
  2. Work on sliding window, start and end, for each end position from 0 to length - 1, run a loop on start as long as the balance string is met.
  3. Work on simplicity in design process and coding process. Right now there are less than 5 things to consider, map, ascii value, slide window, left, right, while loop
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sliding_windows___study_code
{
    class Program
    {
        static void Main(string[] args)
        {
            var result = BalancedString("QWER");
        }

        /// <summary>
        /// Oct. 19, 2019
        /// Leetcode 
        /// study code
        /// https://leetcode.com/problems/replace-the-substring-for-balanced-string/discuss/408978/JavaC%2B%2BPython-Sliding-Window
        /// It is to find all sliding windows containing above average number of chars in "QWER". 
        /// The dictionary to store is whole string except current sliding window. Why? It is so simple 
        /// to write to the code and it can be solved in less than 15 minutes.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int BalancedString(string s)
        {
            var exclusiveSlidingWindow = new Dictionary<int, int>();
            var length = s.Length;
            var minLength = length; 

            for(int i = 0; i < length; i++)
            {
                int key = s[i];
                if(!exclusiveSlidingWindow.ContainsKey(key))
                {
                    exclusiveSlidingWindow.Add(key, 0);
                }

                exclusiveSlidingWindow[key]++; 
            }

            int left = 0; 
            int average = length/4; 

            // Find all sliding window containing each char of "QWER" above average
            for(int index = 0; index < length; index++)
            {
                exclusiveSlidingWindow[s[index]]--;

                while(left < length && 
                    (!exclusiveSlidingWindow.ContainsKey('Q') || exclusiveSlidingWindow['Q'] <= average) && 
                    (!exclusiveSlidingWindow.ContainsKey('W') || exclusiveSlidingWindow['W'] <=average)  && 
                    (!exclusiveSlidingWindow.ContainsKey('E') || exclusiveSlidingWindow['E'] <= average) && 
                    (!exclusiveSlidingWindow.ContainsKey('R') || exclusiveSlidingWindow['R'] <= average))
                {
                    minLength = Math.Min(minLength, index - left + 1); 
                    exclusiveSlidingWindow[s[left]] += 1; // not in the sliding window
                    left++; // caught by online judge
                }
            }

            return minLength == length? 0 : minLength; 
        }
    }
}

No comments:

Post a Comment