-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatingLetter.cs
More file actions
45 lines (41 loc) · 1.07 KB
/
RepeatingLetter.cs
File metadata and controls
45 lines (41 loc) · 1.07 KB
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
public class RepeatingLetter
{
// HackerRank String repeated.
public static long RepeatedString(string s, long n)
{
#region cases where length of string is less than length or required characters
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (s.Length > n)
{
for (int i = 0; i < n; i++)
{
sb.Append(s[i]);
}
s = sb.ToString();
}
#endregion
long iteration = n / s.Length;
long modulus = n % s.Length;
long result = 0;
foreach (char i in s)
{
if (i.ToString() == "a")
{
result++;
}
}
result = iteration > 0 ? result * iteration : result;
if (modulus > 0)
{
for (int i = 0; i < modulus; i++)
{
if (s[i].ToString() == "a")
{
result++;
}
}
}
Console.WriteLine(result);
return result;
}
}