Count of strings where adjacent characters are of difference one in C++


We are given a num number as input. The goal is to count the number of possible strings of length num such that all adjacent characters have difference between ascii values as 1.

If num is 2 then strings will be “ab”, “ba”, “bc”, “cb”, ……..”yz”, “zy”.

Let us understand with examples

Input − num=3

Output − Count of strings where adjacent characters are of difference one are − 98

Explanation − Some sample strings are: “abc”, “aba”, “cde” …..”xyx”, “zyz”, “xyz”.

Input − num=2

Output − Count of strings where adjacent characters are of difference one are − 50

Explanation − Some sample strings are: “ab”, “ba”, “cd” …..”xy”, “zy”, “yz”.

Approach used in the below program is as follows

For length = 2.

String starting with a= “ab”

Strings starting with b= “ba”, “bc”

Strings starting with c= “cd”, “cb”...............

For length = n.

String starting with a=ways of number of strings of length n-1 starting with b

String starting with b=ways of number of strings of length n-1 starting with a or c

String starting with c=ways of number of strings of length n-1 starting with b or d

We will solve this using dynamic programming.

Take an array arr[num+1][27]. Containing a number of strings of length i starting with alphabet number j in arr[i][j]. All arr[1][j] will be 1 for strings “a”, “b”...”z”.

Rest for arr[2 to num+1][0 to 25], set arr[i][j]=arr[i-1][j+1] for j=0. Else set arr[i][j] = arr[i-1][j-1] + arr[i-1][j+1];

The result will be the sum of num-th row counts.

  • Take input integer num

  • Function difference_strings(int num) takes the num and returns the count of strings where adjacent characters are of difference one

  • Take the initial count as 0.

  • Initialize arr[num + 1][27] with all 0s.

  • Initialize arr[1][0 to 25] with all 1’s.

  • Traverse 2D array arr[][] using two for loops from row 2nd to last and columns 0 to 25 for all 26 alphabets.

  • For j=0, the starting character is ‘a’. Set current count as arr[i][j] = arr[i - 1][j + 1];

  • Otherwise set arr[i][j] = (arr[i - 1][j - 1] + arr[i - 1][j + 1])

  • Now after the end of the above loops, traverse last row and add arr[num][ 0 to 25] to count.

  • Return count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int difference_strings(int num){
   long int count = 0;
   long int arr[num + 1][27];
   memset(arr, 0, sizeof(arr));
   for (int i = 0; i <= 25; i++){
      arr[1][i] = 1;
   }
   for (int i = 2; i <= num; i++){
      for (int j = 0; j <= 25; j++){
         if (j == 0){
            arr[i][j] = arr[i - 1][j + 1];
         }
         else{
            arr[i][j] = (arr[i - 1][j - 1] + arr[i - 1][j + 1]);
         }
      }
   }
   for (int i = 0; i <= 25; i++){
      count = (count + arr[num][i]);
   }
   return count;
}
int main(){
   int num = 2;
   cout<<"Count of strings where adjacent characters are of difference one are: "<<difference_strings(num);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of strings where adjacent characters are of difference one are: 50

Updated on: 03-Dec-2020

761 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements