Click here to Skip to main content
15,909,325 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Find the number of occurances of "aa" in "caaab".
The answer should be 2.

What I have tried:

string str = d;
                            for (int number = 0; number < d.Length - genes[i].Length; number++)
                            {
                                var compstr = d.Substring(number, genes[i].Length).ToString();
                                str = d.Substring(1);
                                if (compstr == genes[i])
                                {
                                    count++;
                                }
                            }
Posted
Updated 17-Jul-17 2:13am
Comments
W Balboos, GHB 17-Jul-17 7:48am    
Sounds like homework to me.
londhess 17-Jul-17 8:09am    
Try something like this.
int count = new string(str.Select((c, index) => str.Substring(index).TakeWhile(e => e == c))
.OrderByDescending(e => e.Count())
.First().ToArray()).Count()-1;

This is a simple problem to work with but I get surprised at the simple optimisation that people miss in problems like this. Let's take a look at the code first:
C#
for (int index = 0; index < input.Length - 1; index++)
{
  if (input.Substring(index, 2) == "aa")
  {
    count++;
  }
}
And that's it - a simple solution to a simple problem. Did you notice the problem optimisation though? We know that the length of our test string is 2, so the last place that the match can start is the second off last letter; hence the reason we take 1 off the Length. If the text we were trying to match against was three characters, we would take 2 off the length, and so on.
 
Share this answer
 
string  input = "caaabaaaa";
string find = "aa";
int count = 0;

for (int i = 0; i < input.Length; i++)
{
    if (  i + find.Length <=input.Length) // validate to avoid index error
    {
        var str = input.Substring(i, find.Length);
        if (str == find)
        {
            count++;
        }
    }
}
Console.WriteLine(count); //2
 
Share this answer
 
I think the answer should be 1, here is a nice example: [dotnetperls]
 
Share this answer
 
Comments
Pete O'Hanlon 17-Jul-17 7:58am    
No, he's right. There should be two. There are three a's together in that, so there should be two together. In the word, caaab, the first aa starts at index 1 and the second aa starts at index 2.
RickZeeland 17-Jul-17 8:24am    
That's a matter of opinion I think, if you allow overlapping strings you are right.
Maybe a small example, say I have two strings and append them: "nono" + "nono" then how many nono's do you get ?
RickZeeland 20-Jul-17 9:08am    
To the people downvoting my answer without leaving a comment, I would like to say the following: just use a text editor and try to find 'aa' in the string 'caaab'.
What do you think the result will be ?
Satya Prakash Swain 20-Jul-17 22:25pm    
Initially, i too faced it. Please have a look at this article for pattern searching. http://www.geeksforgeeks.org/searching-for-patterns-set-2-kmp-algorithm/
RickZeeland 21-Jul-17 3:54am    
Looking at the article you and Pete are right, still I find it strange that in practice, using e.g. Notepad++ or the Dotnetperls example, the search results are different.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900