1

the logic behind this was (n-2)3^(n-3) has lots of repetitons like (abc)***(abc) when abc is at start and at end and the strings repated total to 3^4 . similarly as abc moves ahead and number of sets of (abc) increase

10
  • 9
    Welcome to SO Dinesh. Is this a homework? If it is, you can safely tag it as homework, no one will be mad at you :) And you can share your experiences so far in solving the problem. And I think the problem needs some clarification.. Commented Dec 20, 2011 at 13:14
  • If you can get all of the strings, toss them into a set (uses unique keys) and you'll be good for lack of duplicates :) that's the half-assed simple solution though - I'm sure there's a creative way to figure out when ABC are bordering each other that I haven't thought of. Commented Dec 20, 2011 at 13:18
  • 2
    Also, are you sure that this should be tagged with C++ and Java? Which language are you working in? Commented Dec 20, 2011 at 13:19
  • This is not homework . I tried several things . didnt work out . i need an efficient algo ..i m not asking for a code.. Commented Dec 20, 2011 at 13:21
  • @Dinesh Kukreja: Just asking for code is not a good idea on SO, you should tell us what you've tried so far! Commented Dec 20, 2011 at 13:24

3 Answers 3

1

You can use dynamic programming to compute the number of forbidden strings.

The algorithms follow from the observation below:

"Legal string of size n is the legal string of size n - 1 extended with one letter, so that the last three letters of the resulting string are not all distinct."

So if we had all the legal strings of size n-1 we could try extending them to obtain the legal strings of size n.

To check whether the extended string is legal we just need to know the last two letters of the previous string (of size n-1).

In the algorithm we will compute two arrays, where

different[i] # number of legal strings of length i in which last two letters are different
same[i]      # number of legal strings of length i in which last two letters are the same

It can be easily proved that:

different[i+1] = different[i] + 2*same[i]
same[i+1]      = different[i] + same[i] 

It is the consequence of the following facts:

Any 'same' string of size i+1 can be obtained either from 'same' string of size i (think BB -> BBB) or from 'different' string (think AB -> ABB) and these are the only options.

Any 'different' string of size i+1 can be obtained either from 'different' string of size i (think AB-> ABA ) or from the 'same' string in two ways (AA -> AAB or AA -> AAC)

Having observed all this it is easy to write an algorithm that computes the result in O(n) time.

Sign up to request clarification or add additional context in comments.

2 Comments

There is a problem in your reasoning. Given total[i] = same[i] + different[i], and total[i+i] = total[i] * 3 (eg. add a, b, or c to each string). Then by substituting your equations with my equations for total then your total[i+1] would be different[i]*2 + same[i]*3, whereas it should be different[i]*3 + same[i]*3
@Dunes: But in 'different' and 'same' arrays we count the number of legal strings. For legal strings it is not true that total[i+1] = 3*total[i].
0

I suggest you use recursion, and look at two numbers:

  1. F(n), the number of legal strings of length n whose last two symbols are the same.
  2. G(n), the number of legal strings of length n whose last two symbols are different.

Is that enough to go on?

5 Comments

how would the similarity or disimilarity of last two symbols help..and for that i need to generate all the strings..
is it possible to remove the repetition using permutations
@DineshKukreja, do you understand recursion (in the mathematical sense)? The idea here is that there is a simple method to go from the n case to the n+1 case. I've divided the problem into F and G for reasons that should become clear once you get into it. Given F(n) and G(n), can you calculate F(n+1) and G(n+1)?
can u explain me your logic . how will the last two letters help me determine if string conatin abc or not .
@DineshKukreja, You're approaching it from the wrong direction. Imagine you have a legal string, and you want to add one letter to the end of it and keep it legal. Is that enough or should I spell it out?
0

get the ASCII values of the last three letters and add the square values of these letters. If it gives a certain result, then it is forbidden. For A, B and C, it would be fine.

To do this:

1) find out how to get characters from your string.

2) find out how to get ASCII value of a character.

3) Multiply these ASCII values with themselves.

4) Do that for the three letters each time and add their values.

1 Comment

can u pls guide me how yd it help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.