Solved

C# Problem Solving Execerscise

Posted on 2016-11-11
5
67 Views
Last Modified: 2016-11-12
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();
			long n = Convert.ToInt64(Console.ReadLine());
			while (s.Count() < n)
			{

			}
			var cont = s.Count(x => x == 'a');
			Console.WriteLine(cont);
			Console.ReadKey();

Open in new window

0
Comment
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
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 34

Expert Comment

by:it_saige
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 40

Expert Comment

by:Kyle Abrahams
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  

Open in new window

0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 250 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);

Open in new window

0
 
LVL 34

Assisted Solution

by:it_saige
it_saige earned 250 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)
		{
			string s = Console.ReadLine();
			long count, n = Convert.ToInt64(Console.ReadLine());

			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);
			Console.ReadLine();
		}
	}
}

Open in new window


-saige-
0
 

Author Closing Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question