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

March 18, 2016

Problem statement:

https://www.hackerrank.com/challenges/two-strings/submissions/code/18506948

Julia likes to try a new way to train herself to expand C#/ C++ / Java / JavaScript languages, by reading the solutions, followed up with some study about small topics, make learning more fun.

Coding - write code, read code, memorize some classical code, practice it 20 days.

Julia‘s C# implementation:

1. https://gist.github.com/jianminchen/f73e37b764147cd19c7b

Julia reads 10 C# solution, C++, Java, JavaScript solutions, she likes to have this extensive reading on software code, and then, build up her daily routine as a software programmer. Coding is most important skills,
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

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

https://www.hackerrank.com/challenges/two-strings/leaderboard/filter/language=csharp

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?

Hashset, 
Dictionary, 
String.Contains, 
Hashset.Overlap method, 
string.indexOf, 
Hashtable, 
string.Intersect
Using two pointer to solve the problem

2. use hashset - more efficient
https://gist.github.com/jianminchen/acedbb7cb86cf1c00131

read hashset constructor:  <- Excellent, Julia is learning to write new C# code
https://msdn.microsoft.com/en-us/library/bb301504(v=vs.110).aspx

ToList
https://msdn.microsoft.com/en-us/library/bb342261(v=vs.100).aspx

https://msdn.microsoft.com/library/bb534972(v=vs.100).aspx

3. using string.intersect method
https://gist.github.com/jianminchen/cece566bd69963533e80

4. using C#, var, foreach, Any method - interesting to read
https://gist.github.com/jianminchen/50fc6b5a13b7d62dfa1d

5. IEnumerable - interesting to read the code
https://gist.github.com/jianminchen/cdba6a97769db30c2e76

Read IEnumberable:  C++ analog - duck typing
http://stackoverflow.com/questions/8764643/is-there-a-standard-c-equivalent-of-ienumerablet-in-c

IEnumberable - Java analog
http://stackoverflow.com/questions/362367/java-arrays-generics-java-equivalent-to-c-sharp-ienumerablet

6.  Two hashsets
https://gist.github.com/jianminchen/aa87f82f0247c4b59ab6

7. Hashset - overlaps method - code is readable
https://gist.github.com/jianminchen/b28fab138ad778bc4326

read Hashset overlap api - learn some design:
https://msdn.microsoft.com/en-us/library/bb355623(v=vs.110).aspx

8. Array, Array.BinarySearch method - it is not time efficient solution - but code is reusable.
https://gist.github.com/jianminchen/775cf5e16d065e66feae

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

https://gist.github.com/jianminchen/9d80b3995adf0b01f9d7

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

https://gist.github.com/jianminchen/7de04dcfb8740ee19412

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

https://gist.github.com/jianminchen/de154d443a1434094260

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

12. Use Hashtable
https://gist.github.com/jianminchen/213335b4dec3988facfe

13. Use string.Contains() method
https://gist.github.com/jianminchen/6388585c990a42ea76ae

14. Use Dictionary class
https://gist.github.com/jianminchen/95b701e6d2146da13c1d

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
https://gist.github.com/jianminchen/7562c5dd310508d48f58

16. use string.Containstwo dimension array
https://gist.github.com/jianminchen/365eb2b69e93573c0b5d

17. use Dictionary<char, bool>
https://gist.github.com/jianminchen/a91504a793f795886e23

18. use StringBuilder to concatenate output

https://gist.github.com/jianminchen/1fcc46803321f007eb1b

Read the discussion StringBuilder vs string to concatenation
http://stackoverflow.com/questions/1612797/string-concatenation-vs-string-builder-performance

https://support.microsoft.com/en-us/kb/306822

More reading: 1

19. use " " to concatenate two input strings and then .Split them
https://gist.github.com/jianminchen/591738de12e21e591634

More Julia likes:
IEnumerable - 
https://www.hackerrank.com/rest/contests/master/challenges/two-strings/hackers/cdkmoose/download_solution




Statistics:

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

Advice for C# programmers:

Julia enjoyed the study. Very focus, try to understand the solution, how other people think in C#. It is the big world, millions programmers in the world today, you have to go out to reach them, some wisdom out there, teach you to learn C# programming language in very organic way. 

No one person can put together so many ideas for a simple problem. 

Julia is proud of herself, come to some simple idea to train herself in algorithm thinking. Low cost, and easy to access, and good quality code. 

More reading:
18. use StringBuilder to concatenate output

Here are good ideas for Julia to be a better C# developer as the anonymous code writer in 18:
(From webpage: https://support.microsoft.com/en-us/kb/306822)
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 tahn 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. 

时间: 2024-10-12 17:39:45

Hacker Rank: Two Strings - thinking in C# 15+ ways的相关文章

Machine Learning for Developers

Machine Learning for Developers Most developers these days have heard of machine learning, but when trying to find an 'easy' way into this technique, most people find themselves getting scared off by the abstractness of the concept of Machine Learnin

bzoj 4556: [Tjoi2016&amp;Heoi2016]字符串

二分ans,二分区间长度,st表查,最后主席树判断. 这题最大收获学到了一个nb的卡常技巧,主席树元素个数为0直接返回,不敢相信快了一倍. 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #define N 200005 6 using namespace std; 7 inline int read() 8 { 9 int p=0;cha

poj1733(种类并查集+离散化)

题目链接: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第y个字符中间1的个数为偶数个, x, y, odd表示第x到第y个字符中间1的个数为奇数个, 若m句话中第k+1是第一次与前面的话矛盾, 输出k; 思路: 若x, y之间1的个数为偶数个, 那么1~x 与1~y中1的个数同奇偶性, 反之则异奇偶性, 我们可以将其理解为若输入x, y, even, 即

一些算法刷题的网站

1. leetcode http://leetcode.com/ 2. careerup http://www.careercup.com/ http://hawstein.com/posts/ctci-solutions-contents.html 3. glassdoor http://www.glassdoor.com/index.htm 4. topcoder http://www.topcoder.com/ 5. zoj http://acm.zju.edu.cn/onlinejudg

Python中Unicode码和非Unicode码引起的错误与格式转换

1.1. 问题 Problem You need to deal with data that doesn't fit in the ASCII character set. 你需要处理不适合用ASCII字符集表示的数据. 1.2. 解决 Solution Unicode strings can be encoded in plain strings in a variety of ways, according to whichever encoding you choose: Unicode

一些题目

http://blog.csdn.net/zheng0518/article/details/39189805 给定一个字符串,得到这个字符串中首先出现两次的那个字符 方法一: 1 public static void main(String[] args) { 2 Scanner scanner = new Scanner(System.in); 3 String src = scanner.next(); 4 HashMap<Character,Integer> items = new H

Latex 表格(跨行、跨列、背景加灰)new

一. 效果如图 二.代码如下 1 \begin{table} 2 \centering 3 \caption{Suspiciousness of statements for \texttt{foo()} computed by different approaches} 4 \label{Tbl:Suspiciousness} 5 \begin{tabular}{|c||c|c|c|c||c|c|c|c|} 6 \hline 7 \multirow{2}{*}{statement} & \mu

spoj 7258

Lexicographical Substring Search Time Limit:  1000MS Memory Limit:  Unknown 64bit IO Format:  %lld & %llu Description Little Daniel loves to play with strings! He always finds different ways to have fun with strings! Knowing that, his friend Kinan de

AVICAP (全)

unit vfw; interface usesWindows, MMSystem, Messages; const// ------------------------------------------------------------------// Window Messages WM_CAP... which can be sent to an AVICAP window// ------------------------------------------------------