Sunday, March 13, 2016

HackerRank: string algorithm - Anagram

March 13, 2016

Anagram

Julia's C# implementation:
https://gist.github.com/jianminchen/f3c48ed9f3b16e8c5928

Julia made a few tries before she noticed that she needs to figure out the formula:

for (int i = 0; i < SIZE; i++)
            {
                if (sumA[i] > 0)
                    // add count of chars in array sumA but not in sumB
                    // axxbbbxx, 
                    // axxb -> bbxx, change a to b, that is it!
                    // axxb  a 1, b 1, x 2
                    // bbxx, a 0, b 2, x 2
                    // formula -> 
                    count += (sumA[i] >= sumB[i]) ? (sumA[i] - sumB[i]) : 0;   
            }

Another approach is to add all the differences, and then, divided by 2

C# submission code to study:

1. String class contains, Split functions etc. 
Split function - return array length to get the count of any char in the string. 
https://gist.github.com/jianminchen/adbfc2d809fb5b2bac78

2. add all the difference between two strings, and then divide it by 2
https://gist.github.com/jianminchen/7bbe86bbb83787d6b98b

3. Using Dictionary, KeyValuePair class
https://gist.github.com/jianminchen/78346475b6a7ce5d1681

4. Read more Lambda expression code in C#
https://gist.github.com/jianminchen/9d121bd95266db41dfa8

5. using StringBuilder, C# code
https://gist.github.com/jianminchen/d972656068fa8088ae70

6. use string.Remove function
https://gist.github.com/jianminchen/65687cefd2b107ec5e23


Java Code:
1. https://gist.github.com/jianminchen/794ffee7726df6062a1f

2. Maybe not smart idea, but it works - declare a string
String alph = "abcdefghijklmnopqrstuvwxyz";

https://gist.github.com/jianminchen/0cfa60bac880f2bba10f


Julia likes to read code, any language in submission. She could 
not stop reading, she has read more than 50 solutions, totally opened to so many creative ideas. 

No comments:

Post a Comment