Hackerrank – Weighted Uniform Strings

Hackerrank – Problem Statement

A description of the problem can be found on Hackerrank.

Solution

I will find a list of all substrings, which contains same following characters. Then I will create all weights – When a substring has lengths of n, I will create n parts. This will give me all weights of a substring.

At the end I will find if the weights contains any of the given queries.

Example
"abccddde":
– all substrings
— “a” (weight of 1 unit for a character)
— “b” (weight of 2 units for a character)
— “cc” (weight of 3 units for a character)
— “ddd” (weight of 4 units for a character)
— “e” (weight of 5 units for a character)

All weights for all substring parts
– “a” -> 1
– “b” -> 2
– “cc” -> 1 * 3, 2 * 3 -> 3, 6
– “ddd” -> 1 *4, 2 * 4, 3 * 4 -> 4, 8, 12
– “e” -> 5

All weights : 1, 2, 3, 4, 5, 6, 8, 12

I created solution in:

All solutions are also available on my GitHub profile.

Scala

Java

Javascript

Leave a Reply

Your email address will not be published. Required fields are marked *