Friday, March 25, 2016

HackerRank: Two string - thinking in Java

March 25, 2016

Read other people's ideas. Understand other people by reading their code. Julia likes to read some Java programming language code for 1-2 hours, she came cross people's code, amazed by ideas from people working in Facebook, Amazon, and amazed that people have GOLD prize on HackerRank.

Julia likes to be able to write very readable, most understandable code with shortest time as possible.

problem statement:

https://www.hackerrank.com/challenges/two-strings

Here are a few Java solution she likes:

Solution 1:
https://gist.github.com/jianminchen/6f029bb909ed7d85c012

Solution 2:
source code reference: 
https://www.hackerrank.com/winger

People like to use bit operation, most likely they are expert and then have some Gold in HackerRank. 

bit operation
https://gist.github.com/jianminchen/3d2acf64725cd73c79db


use one integer to store 26 characters, 1 bit one char from a to z.

private static int f(String a) {
        int r = 0;
        for (char c : a.toCharArray()) {
            r |= 1 << (c - 'a');    // Julia's comment: 1 left shift (c-'a') times 
        }
        return r;
    }

Solution 3:

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

Solution 4:

It is interesting to read InputStream, need to warm up on Java class

https://gist.github.com/jianminchen/21ffc64093fd5952647c

Solution 5:
use HashSet, Set, Character, String classes
https://gist.github.com/jianminchen/1eb02a940f7fa4b6a3f7


No comments:

Post a Comment