-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.cs
53 lines (48 loc) · 2.05 KB
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Problem: https://www.hackerrank.com/challenges/weighted-uniform-string/problem
C# Language Version: 7.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts :
- first create a hash set of sum of all possible uniform strings as we iterate through the string char by char
- for each query, just check if the uniform sum being queried is present in the hash set or not (contributes O(1) as searching
in hashset is contant operation)
Time Complexity: O(n) //one iteration is required to create set of weights of uniform stings
Space Complexity: O(n) //space for has set of weights of uniform strings.
*/
using System;
using System.Collections.Generic;
class Solution
{
static void Main(string[] args)
{
var currentChar = Console.Read();
var previousChar = -1;
var uniformStringSum = 0;
var uniformWeightSet = new HashSet<int>();
//special handling for hacker rank execution environment
//while running it on my own Windows based computer, I compare it with ascii code of carriage return ('\r') which is 13.
while (currentChar != 10)
{
if (currentChar != previousChar)
uniformStringSum = currentChar - 96;
else
uniformStringSum += currentChar - 96;
uniformWeightSet.Add(uniformStringSum);
previousChar = currentChar;
currentChar = Console.Read();
}
//skipping '\n' character as new line character in windows is combination of \r and \n
//Commented as it is not required for HackerRank execution environment.
//Console.Read();
var queryCount = int.Parse(Console.ReadLine());
while (queryCount > 0)
{
var uniformWeight = int.Parse(Console.ReadLine());
if (uniformWeightSet.Contains(uniformWeight))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
}