We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
First calculate the weight at all positions of the string as a pre-processing step.
For the weight calculation,
1. If it is not the first element and if its previous character is same as the current character, then add the weight of current character to the previous character's stored weight and store it in array x.
2. Else store the weight of the current character in the array x
For eg. in string "acc"
1st position will have weight equal to the weight of the character i.e. 1 (a=1)
2nd position character (c) and its previous character (a) do not match hence weight of current character (c) will be stored to the array i.e. 3 (c=1)
3rd position character (c) and its previous character (c) match hence weight of current character (c) i.e. 3 will be added to previous weight (3) totalling to 6
Now the x array will have contents as [1, 3, 6]
We make a hash set out of this array for faster checking of presence of a number
Now, for every input, we just need to check whether it is present in the hash set or not and print Yes or No accordingly.
Weighted Uniform Strings
You are viewing a single comment's thread. Return to all comments →
First calculate the weight at all positions of the string as a pre-processing step.
For the weight calculation,
1. If it is not the first element and if its previous character is same as the current character, then add the weight of current character to the previous character's stored weight and store it in array x.
2. Else store the weight of the current character in the array x
For eg. in string "acc"
1st position will have weight equal to the weight of the character i.e. 1 (a=1)
2nd position character (c) and its previous character (a) do not match hence weight of current character (c) will be stored to the array i.e. 3 (c=1)
3rd position character (c) and its previous character (c) match hence weight of current character (c) i.e. 3 will be added to previous weight (3) totalling to 6
Now the x array will have contents as [1, 3, 6]
We make a hash set out of this array for faster checking of presence of a number
Now, for every input, we just need to check whether it is present in the hash set or not and print Yes or No accordingly.