iven a number n, count number of strings of length n such that every string has adjacent characters with difference between ASCII values as 1.

**Examples**:

Input : N = 1 Output : Total strings are 26 Explanation : For N=1, strings are a, b, c,, ...., x, y, z Input : N = 2 Output : Total strings are 50 Explanation : For N = 2, strings are ab, ba, bc, cb, .., yx, yz, zy

For strings starting with character ‘A’ and length ‘i’, we consider all strings of length ‘i-1’ and starting with character ‘B’

For strings starting with character ‘G’ and length ‘i’, we consider all strings of length ‘i-1’ and starting with character ‘H’ and all strings of length ‘i-1’ and starting with ‘F’.

We take the base case for n = 1, and set result for all 26 characters as 1. This simply means when 1 character string is consider all alphabets from a-z are taken only once.

For **N = 2**,

For **N = 3**,

**Conclusion** : For **N = n**

countAdjacent(n)dp[i][j] finally stores count of strings of length i and starting with character j. Initialize dp[n+1][27] as 0 Initialize dp[1][j] = 1 where j = 0 to 25 for i = 2 to n for j = 0 to 25 if (j = 0) dp[i][j] = dp[i-1][j+1]; else dp[i][j] = dp[i-1][j-1] + dp[i-1][j+1]; Sum of n-th row from 0 to 25 is the result.

## C++

`// CPP Program to count strings with adjacent ` `// characters. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `countStrs(` `int` `n) ` `{ ` ` ` `long` `int` `dp[n + 1][27]; ` ` ` ` ` `// Initializing arr[n+1][27] to 0 ` ` ` `memset` `(dp, 0, ` `sizeof` `(dp)); ` ` ` ` ` `// Initialing 1st row all 1 from 0 to 25 ` ` ` `for` `(` `int` `i = 0; i <= 25; i++) ` ` ` `dp[1][i] = 1; ` ` ` ` ` `// Begin evaluating from i=2 since 1st row is set ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` `for` `(` `int` `j = 0; j <= 25; j++) ` ` ` ` ` `// j=0 is 'A' which can make strings ` ` ` `// of length i using strings of length ` ` ` `// i-1 and starting with 'B' ` ` ` `if` `(j == 0) ` ` ` `dp[i][j] = dp[i - 1][j + 1]; ` ` ` `else` ` ` `dp[i][j] = (dp[i - 1][j - 1] + ` ` ` `dp[i - 1][j + 1]); ` ` ` `} ` ` ` ` ` `// Our result is sum of last row. ` ` ` `long` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 0; i <= 25; i++) ` ` ` `sum = (sum + dp[n][i]); ` ` ` `return` `sum; ` `} ` ` ` `// Driver's Code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `cout << ` `"Total strings are : "` `<< countStrs(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to count strings ` `// with adjacent characters. ` `class` `GFG ` `{ ` ` ` ` ` `static` `long` `countStrs(` `int` `n) ` ` ` `{ ` ` ` `long` `[][] dp = ` `new` `long` `[n + ` `1` `][` `27` `]; ` ` ` ` ` `// Initializing arr[n+1][27] to 0 ` ` ` `for` `(` `int` `i = ` `0` `; i < n + ` `1` `; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `0` `; j < ` `27` `; j++) ` ` ` `{ ` ` ` `dp[i][j] = ` `0` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Initialing 1st row all 1 from 0 to 25 ` ` ` `for` `(` `int` `i = ` `0` `; i <= ` `25` `; i++) ` ` ` `{ ` ` ` `dp[` `1` `][i] = ` `1` `; ` ` ` `} ` ` ` ` ` `// Begin evaluating from i=2 ` ` ` `// since 1st row is set ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// j=0 is 'A' which can make strings ` ` ` `for` `(` `int` `j = ` `0` `; j <= ` `25` `; j++) ` ` ` ` ` `// of length i using strings of length ` ` ` `// i-1 and starting with 'B' ` ` ` `{ ` ` ` `if` `(j == ` `0` `) ` ` ` `{ ` ` ` `dp[i][j] = dp[i - ` `1` `][j + ` `1` `]; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `dp[i][j] = (dp[i - ` `1` `][j - ` `1` `] ` ` ` `+ dp[i - ` `1` `][j + ` `1` `]); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Our result is sum of last row. ` ` ` `long` `sum = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i <= ` `25` `; i++) ` ` ` `{ ` ` ` `sum = (sum + dp[n][i]); ` ` ` `} ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` `System.out.println(` `"Total strings are : "` `+ ` ` ` `countStrs(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 Program to count strings with ` `# adjacent characters. ` `def` `countStrs(n): ` ` ` ` ` `# Initializing arr[n+1][27] to 0 ` ` ` `dp ` `=` `[[` `0` `for` `j ` `in` `range` `(` `27` `)] ` ` ` `for` `i ` `in` `range` `(n ` `+` `1` `)] ` ` ` ` ` `# Initialing 1st row all 1 from 0 to 25 ` ` ` `for` `i ` `in` `range` `(` `0` `, ` `26` `): ` ` ` `dp[` `1` `][i] ` `=` `1` ` ` ` ` `# Begin evaluating from i=2 since ` ` ` `# 1st row is set ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `): ` ` ` `for` `j ` `in` `range` `(` `0` `, ` `26` `): ` ` ` ` ` `# j=0 is 'A' which can make strings ` ` ` `# of length i using strings of length ` ` ` `# i-1 and starting with 'B' ` ` ` `if` `(j ` `=` `=` `0` `): ` ` ` `dp[i][j] ` `=` `dp[i ` `-` `1` `][j ` `+` `1` `]; ` ` ` `else` `: ` ` ` `dp[i][j] ` `=` `(dp[i ` `-` `1` `][j ` `-` `1` `] ` `+` ` ` `dp[i ` `-` `1` `][j ` `+` `1` `]) ` ` ` ` ` `# Our result is sum of last row. ` ` ` `sum` `=` `0` ` ` `for` `i ` `in` `range` `(` `0` `, ` `26` `): ` ` ` `sum` `=` `sum` `+` `dp[n][i] ` ` ` ` ` `return` `sum` ` ` `# Driver's Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n ` `=` `3` ` ` `print` `(` `"Total strings are : "` `, countStrs(n)) ` ` ` `# This code is contributed by Sairahul Jella ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to count strings with ` `// adjacent characters. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `long` `countStrs(` `int` `n) ` ` ` `{ ` ` ` `long` `[,] dp = ` `new` `long` `[n + 1, 27]; ` ` ` ` ` `// Initializing arr[n+1][27] to 0 ` ` ` `for` `(` `int` `i = 0; i < n + 1; i++) ` ` ` `for` `(` `int` `j = 0; j < 27; j++) ` ` ` `dp[i, j] = 0; ` ` ` ` ` `// Initialing 1st row all 1 from 0 to 25 ` ` ` `for` `(` `int` `i = 0; i <= 25; i++) ` ` ` `dp[1, i] = 1; ` ` ` ` ` `// Begin evaluating from i=2 since 1st row is set ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j <= 25; j++) ` ` ` ` ` `// j=0 is 'A' which can make strings ` ` ` `// of length i using strings of length ` ` ` `// i-1 and starting with 'B' ` ` ` `if` `(j == 0) ` ` ` `dp[i, j] = dp[i - 1, j + 1]; ` ` ` `else` ` ` `dp[i, j] = (dp[i - 1, j - 1] + ` ` ` `dp[i - 1, j + 1]); ` ` ` `} ` ` ` ` ` `// Our result is sum of last row. ` ` ` `long` `sum = 0; ` ` ` `for` `(` `int` `i = 0; i <= 25; i++) ` ` ` `sum = (sum + dp[n, i]); ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 3; ` ` ` `Console.Write(` `"Total strings are : "` `+ countStrs(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by DrRoot_ ` |

*chevron_right*

*filter_none*

**Output:**

Total strings are : 98

This article is contributed by **Shubham Rana**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of strings that become equal to one of the two strings after one removal
- String with k distinct characters and no same characters adjacent
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Count of N size strings consisting of at least one vowel and one consonant
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Sub-strings that start and end with one character and have at least one other
- Strings formed from given characters without any consecutive repeating characters
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Meta Strings (Check if two strings can become same after a swap in one string)
- Minimum string such that every adjacent character of given string is still adjacent
- Count binary strings with k times appearing adjacent two set bits
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count changes in Led Lights to display digits one by one
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Longest subsequence such that adjacent elements have at least one common digit
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Rearrange characters in a string such that no two adjacent are same
- Minimum replacements to make adjacent characters unequal in a ternary string
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2