

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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
- Related Questions & Answers
- Program to count number of permutations where sum of adjacent pairs are perfect square in Python
- Count of strings that become equal to one of the two strings after one removal in C++
- Count of arrays in which all adjacent elements are such that one of them divide the another in C++
- Count common characters in two strings in C++
- Maximum sum of difference of adjacent elements in C++
- Convert list of strings and characters to list of characters in Python
- Program to find total number of strings, that contains one unique characters in Python
- Order strings by length of characters IN mYsql?
- Count binary strings with k times appearing adjacent two set bits in C++
- Find uncommon characters of the two strings in C++
- SELECT * WHERE var == [one of many alternatives] in MySQL?
- ASCII sum difference of strings in JavaScript
- Program to find minimum possible difference of indices of adjacent elements in Python
- Python – Filter Tuples with Strings of specific characters
- Partition N where the count of parts and each part are a power of 2, and part size and count are restricted in JavaScript