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. 

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

  1. Hacker Rank: Kingdom Division 不完全报告

    原题链接: Kingdom Division 由于树的层次可能很深,所以这里不能使用递归版的DFS.我使用了BFS. BFS确定各结点的父结点和它的孩子数. 用逆拓扑排序确定结点的计算顺序. same ...

  2. Go语言strings包

    目录 strings包实现了用于操作字符的简单函数. func EqualFold 判断两个utf-8编码字符串(将unicode大写.小写.标题三种格式字符视为相同)是否相同. func Equal ...

  3. Consuming JSON Strings in SQL Server

    https://www.simple-talk.com/sql/t-sql-programming/consuming-json-strings-in-sql-server/ Consuming JS ...

  4. Machine Learning for Developers

    Machine Learning for Developers Most developers these days have heard of machine learning, but when ...

  5. JavaScript函数式编程究竟是什么?

    摘要: 理解函数式编程. 作者:前端小智 原文:JS中函数式编程基本原理简介 Fundebug经授权转载,版权归原作者所有. 在长时间学习和使用面向对象编程之后,咱们退一步来考虑系统复杂性. 在做了一 ...

  6. 数据结构与算法分析 – Disjoint Set(并查集)

    什么是并查集?并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题. 并查集的主要操作1.合并两个不相交集合2.判断两个元素是否属于同一集合 主要操作的解释 ...

  7. SPOJ220 Relevant Phrases of Annihilation(后缀数组)

    引用罗穗骞论文中的话: 先将n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组.然后二分答案,再将后缀分组.判断的时候,要看是否有一组后缀在每个原来的字符串中至少出现两次,并 ...

  8. Effective Java 51 Beware the performance of string concatenation

    Using the string concatenation operator repeatedly to concatenate n strings requires time quadratic ...

  9. POJ 1795

    DNA Laboratory Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 1425   Accepted: 280 Des ...

随机推荐

  1. 关于几个主流语音SDK的接入问题

    这两周都在忙着游戏上线还有接入游戏语音,两周分别接了腾讯语音和百度语音!!! 关于腾讯语音的一些问题 由于发现腾讯语音的在录完音频后的数据是编过码的所以出现了一些问题: *不能解码(腾讯方不提供解码算 ...

  2. 深入浅出Redis-redis底层数据结构(上)

    1.概述 相信使用过Redis 的各位同学都很清楚,Redis 是一个基于键值对(key-value)的分布式存储系统,与Memcached类似,却优于Memcached的一个高性能的key-valu ...

  3. 浅谈WEB页面提速(前端向)

    记得面试现在这份工作的时候,一位领导语重心长地谈道——当今的世界是互联网的世界,IT企业之间的竞争是很激烈的,如果一个网页的加载和显示速度,相比别人的站点页面有那么0.1秒的提升,那也是很大的一个成就 ...

  4. Web Api 入门实战 (快速入门+工具使用+不依赖IIS)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...

  5. Spring框架概述

    Spring是最流行的Java企业级应用开发框架,全球数以百万的开发者在使用Spring框架创建高性能.易测试.可重用的代码. Spring框架的核心特性可以应用于任何Java应用,但扩展的JavaE ...

  6. Linux 添加新磁盘,在线扩充空间

    CentOS 7开发环境中的home 目录空间满了,需要增加空间 到虚拟机上执行"ls /sys/class/scsi_host",然后重新扫描SCSI总线来添加设备.如右图.然后 ...

  7. Servlet监听器笔记总结

    监听器Listener的概念 监听器的概念很好理解,顾名思义,就是监视目标动作或状态的变化,目标一旦状态发生变化或者有动作,则立马做出反应. Servlet中的也有实现监听器的机制,就是Listene ...

  8. ThinkPHP+Smarty模板中截取包含中英文混合的字符串乱码的解决方案

    好几天没写博客了,其实有好多需要总结的,因为最近一直在忙着做项目,但是困惑了几天的Smarty模板中截取包含中英文混合的字符串乱码的问题,终于解决了,所以记录下来,需要的朋友看一下: 出现乱码的原因: ...

  9. 以向VS 程序打包集成自动写入注册表功能为例,介绍如何实现自由控制安装过程

    最近由于项目部署时需要更灵活的控制程序安装的流程以及自定义安装行为,特意研究了一下VS程序打包,把解决办法和大家分享一下. 以VS2010为例: 这是一个已经设置好最基本的Visual Studio ...

  10. C# 对象实例化 用json保存 泛型类 可以很方便的保存程序设置

    参考页面: http://www.yuanjiaocheng.net/webapi/test-webapi.html http://www.yuanjiaocheng.net/webapi/web-a ...