I solved a coding challenge in HackerRank today. The challenge is called The Love Letter Mystery. Check out my solution below.
I think my solution is not elegant 😅 I could improve it.
The solution idea:
- inputs is an array of String.
- each element is just a String type object. A String type object is a collection of Character type.
- Convert a String to an array of Int type. e.g. [“a”, “b”, “c”] -> [97, 98, 99]
- Also create the reversed version. [“c”, “b”, “a”] -> [99, 98, 97]
- Compare each element. the first element of the normal version should be compared with the first element of the reversed version.
- If the reversed version’s element is greater than the normal, get the difference of the value. The value is how many times to decrement the reversed version to match with the normal.
- Sum up the difference values.