Sunday, February 28, 2016

code challenge: count of substring

February 28, 2016

Problem statement

Problems solved in the progression of coding:
1. Runtime error - exceed time limit
   naive solution - compare each substring if it contains 00 or 11
2. Console.ReadLine only reads up to 256 chars, the input is up to 100000 chars.
3. Recursive calls - stack overflow - string length is up to 100000
4. Using iterative solution to replace recursive solution

First, wrote a solution in 20 minutes, but Time exceeding limit - TLE error.
Solution 1: C# code


Solution 2: C# code

So, write second version using recursive to avoid redundant calculation: stack overflow problem

Solution 3: C# code

Then, wrote third version with iterative solution:

Solution 4: C# code
(HackerRank embedded C# executable - wrong answer, but Visual express is ok! Cannot figure out! )


Spent more than 4 hours on this easy question. Totally invest 3 hours nonstop on Sunday afternoon on this problem solving.

What we say to encourage this behavior - have guts to fail. This is just the practice.

March 7, 2017

Need to review last practice and find out a solution.

No comments:

Post a Comment