Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

C# Problem Solving Execerscise

Posted on 2016-11-11
Medium Priority
83 Views
I have been trying to solve this problem https://www.hackerrank.com/challenges/repeated-string and i have tryed to many things but
i 'm stuck so can you help me solving it ?

the main problem here is i want the chars inside the s string to append on itself in a short duration and the size of the string = n and then calculate how many Char 'a' in the string

``````string s = Console.ReadLine();
while (s.Count() < n)
{

}
var cont = s.Count(x => x == 'a');
Console.WriteLine(cont);
``````
0
Question by:Omar Alaa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 34

Expert Comment

ID: 41884235
Programming is as much about solving the problem as it is about finding the most efficient solution.  I can tell you that the can be solved by calculating the results (which is more efficient) as opposed to creating the infinite string and then counting the characters.

-saige-
0

LVL 41

Expert Comment

ID: 41884248
Yeah, you'll definitely run into timeout errors.

I'm close in my solution but it still needs work:

``````//test 9;
string s = "epsxyyflvrrrxzvnoenvpegvuonodjoxfwdmcvwctmekpsnamchznsoxaklzjgrqruyzavshfbmuhdwwmpbkwcuomqhiyvuztwvq";
long n = 549382313570;

long countA = 0;
foreach (char c in s.ToCharArray())
{
if (c == 'a')
countA++;
}

long result = 0;

if (countA > 0)
{
//find out the amount of times A appears as a proportion.
// so if test 1 is aba  and 10
// 2 / 3 == .66 * 10 = 6 times.
result = (long)Math.Floor(((double)countA / s.Length) * n);

//then process the leftovers.
for (int i = 0; i < (int) n % s.Length; i++)
{
if (s[i] == 'a')
result++;
}
}
Console.WriteLine(result);  // expected Result of 16481469408
``````
0

LVL 41

Accepted Solution

Kyle Abrahams earned 1000 total points
ID: 41884255
Apparently there was a problem with my math.

SPOILER DON'T READ THIS CODE IF YOU DON'T WANT THE SOLUTION.

``````  long countA = 0;
foreach (char c in s.ToCharArray())
{
if (c == 'a')
countA++;
}

long result = 0;

if (countA > 0)
{
result = (long)Math.Floor((double)countA * (n / s.Length));

for (int i = 0; i < (int) (n % s.Length); i++)
{
if (s[i] == 'a')
result++;
}
}

Console.WriteLine(result);
``````
0

LVL 34

Assisted Solution

it_saige earned 1000 total points
ID: 41884257
As Kyle let the cat out of the bag...  ;)

Here was my implementation (same spoiler as above):
``````using System;
using System.Linq;

namespace EE_Q28982570
{
class Program
{
static void Main(string[] args)
{

if (string.IsNullOrEmpty(s) || n < 0)
Console.WriteLine(0);

count = (n / s.Length) * s.Count(c => c == 'a');
for (int i = 0; i < n % s.Length; i++)
{
if (s[i] == 'a')
{
count++;
}
}
Console.WriteLine(count);
}
}
}
``````

-saige-
0

Author Closing Comment

ID: 41884668
Thank you guys. and thank you for respecting the Spoil for answers . you were really helpful
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month11 days, 19 hours left to enroll