Friday, March 18, 2016

Hacker Rank: Two Strings - thinking in C# 15+ ways

March 18, 2016

Problem statement is here.

Introduction


Julia likes to try a new way to train herself to expand C#, C++ , Java, and JavaScript languages, by reading the solutions first, followed up with a study to feed curiosity, make learning more fun.

Read other people's code! It may be big surprise. Julia is out-of-date to catch up C# skills, when she read those lamba function and LINQ code, she was shocked how concise it is. 

Writing this blog is to help herself to advance her skills in language C#, the more she writes about her learning process, the more she involves with the community, she gets much smart every day. You do not believe it? -:)

Julia's C# implementation is here.

Thinking in all languages


Let us go over some submissions and then write down what players do to solve the problem.

Julia reads 5 different languages at least 10 solution each, total more than 50 solutions.  To learn C#, C++, Java, JavaScript, she likes to have this extensive reading on algorithm code submissions, and then build up her daily routine as a software programmer. Read code is most challenging and exciting work to build up a good programmer. 

A list for each language in her arsenal:

C#    - Read first, then write
C++  - Read more C++ code, prepare to write any time
Java  - Read more
JavaScript - Julia tries to read more JavaScript code, focus on reading for next 2 months

Thinking in C# 15+ ways


How to think about 15+ ways in C#? Enumerate the class and API you like to solve the algorithm two strings. Are you ready for this?


She also likes the following C# solution by other submissions:

Here is the link to go over all C# solution on two string algorithm on Hackerrank.com, called two string C# solutions.

Do it yourself before code tour


Before you read the solution, can you think about using C# Hashset, Dictionary, String.Contains, Hashset.Overlap method, string.indexOf, Hashtable, string.Intersect etc. solve the problem?

Dictionary

Hashset
  Hashset.Overlap 

Hashtable

String 
  String.Contains
  string.indexOf 
  string.Intersect

Using two pointer to solve the problem


Code tour



20 code submissions are listed to study. C# classes are used in the following orders:

Array
Dictionary
Enumerable
HashSet
HashTable
IEnumerable
String
StringBuilder

APIs are most important things to learn in coding. 

1. Hashset
use hashset - more efficient, code is here

read hashset constructor on Microsoft website. Excellent, Julia is learning to write new C# code


2. ToList

Read Enumerable.ToList<Source> method on Microsoft site first. Link is here.

Read Enumerable.ToAny<Source> method on Micrsoft site, link is here.

3. string.intersect API

Code submission is here to study. 

4. using C#, var, foreach, Any method - interesting to read

Code is here to study. 

5. IEnumerable - interesting to read the code

Code is here to study. 

Read IEnumberable:  C++ analog - duck typing, a question on stackoverflow.com. Link is here.

IEnumberable - Java analog

stackoverflow question is here.


6.  Two hashsets

Code is here to study


7. Hashset - overlaps method - code is readable

Code is here to study. 

Hashset overlap API
MSDN document is here.

8. Array, Array.BinarySearch method - it is not time efficient solution - but code is reusable.

Code is here to study.

9. Declare new struct data, use byte type, and code is beautiful, succinct.

Code is here to study.


10. use two pointers, move forward if need separately - can be reused.

Code is here to study.


11. Two things Julia likes:
'z'-'a' in array declaration,
second one is to use one array int[26], not 2; second one is char array

Code is here.

 bool[] founded = new bool['z' - 'a' + 1];
 char[] secondWord = Console.ReadLine().ToCharArray();

12. Use Hashtable

Code is here.


13. Use string.Contains() method

Code is here


14. Use Dictionary class

Code is here


Dictionary<int, char>
Dictionary<int,char> dex = new Dictionary<int,char>();
Dictionary method ContainsValue()

15. string.ToCharArray(), Distinct() of Char Array, ToList(), ToArray(), string.IndexOf methods

Code is here

16. Use string.Contains, two dimension array

Code is here

17. use Dictionary<char, bool>

Code is here.

18. use StringBuilder to concatenate output

Code is here


Read the discussion StringBuilder vs string to concatenation
a question on stackoverflow.com, the article is here to read. 

One more article to read:



19. use " " to concatenate two input strings and then .Split them

Code is here


Last one, 20:


IEnumerable -  Code is here


StringBuilder


Read StringBuilder article on support.microsoft.com. Here is the article link. 

Take some notes here:

1. the benefits of using the StringBuilder class over traditional concatenation techniques

2. C/C++ strcat() - to allocate a large character array as a buffer and copy string data into the buffer.

3. In .NET framework, a string is immutable; it cannot be modified in place. <- immutable explanation!

4. The C# + concatenation operator builds a new string and causes reduced performance when it concatenates large amounts of text. 

5. .NET framework, a StringBuilder class is optimized for string concatenation. 

same as using a character array in C/C++, as well as automatically growing the buffer size (if needed) and tracking the length for you. 

6. Reuse existing StringBuilder class rather than reallocate each time you need one. This limits the growth of the heap and reduces garbage collection. StringBuilder makes more efficient use of the heap than using the + operator. 


Statistics



More than 3 hours to work on this study, thinking in C#.



Future master of C# programmers

  

It is the big world since millions programmers are working right now. How to advance the skills to compete? 

The idea of training on hackerrank is simple,  easy to access, and the resource is good since the code passes all test cases on hackerrank, and it is free. 

The study of thinking in 15+ ways are a good start for Julia to master C# programming language. First Julia has to reach out to the community to see how other people think in C#, and then document the study, and then understand the thinking process in all the ways. 

Julia likes to be the master of C# programming language, and also a good problem solver. Every idea counts. 


Follow up 



May 5, 2017

Spent more than 2 hours to clean up the blog, make it more readable, fix issues on styles like font color, size, layout, subheading etc. 

Practice those C# solution. 

Compare hackerrank code submissions study with other choices, such as taking courses or reading a book on C#. 


No comments:

Post a Comment