recategorized by
2,683 views
0 votes
0 votes
Lets for a a given string aabbbccdd

I need to find the number of substrings possible how to go about it? Does the n(n+1)/2 formula work here also?
recategorized by

1 Answer

1 votes
1 votes

(SUBSTRING: IS CONTIGUOUS SEQUENCE OF CHARACTERS OF A STRING  )

We know :  general formula to find number of sub strings  for given string (unique symbols) is : n*(n+1)/2 .

But, for the strings containing duplicates. Here's the way you can solve it.

First: Number of substrings with length 0 : 1

Second: Number of substrings with length 1: 4     { 'a', 'b' , 'c' , 'd' }

Now the problem arises for the substrings of length 2 and more.... 

 

let's consider a a b b b c c d d as position 1 2 3 4 5 6 7 8 9.

Substring of length 2, 3 , 4... will be created by choosing two points (positions).

E.g: a a b b b c c d d

       1 2 3 4 5 6 7 8 9   for length 2 : 1-2   2-3    3-4    4-5.....are selected i.e aa ab ... bb bb bc cc....

                      similarly for length 3:   1-3  2-4   3-5.... are selected i.e aab abb bbb....

so, you can see we just have to choose any two positions i and j and find number of combinations i.e 9C2

but we must have to remove dublicate substrings also... which is created by bb and bb only (i.e postion 3-4 and 4-5 only)

so Answer is 1 + 4 + (9C2-1)  = 1 + 4 + 35 = 40

Related questions

3 votes
3 votes
2 answers
1
Akriti sood asked Nov 7, 2016
6,682 views
A palindrome is a string whose reversal is identical to the string. How many bit strings of length n are palindromes? 2⌈n⁄2⌉ 2(⌊ n/2⌋ ) 2⌈n⁄2⌉ -1 2(�...
0 votes
0 votes
2 answers
4
rohankrishan asked Jun 29, 2022
248 views
Example: 11110100000111 should be accepted. There are 6 zeros. 6 is divisble by 2 and 3. This machine required at least six states.