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.
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] as 0 Initialize dp[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.
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 firstname.lastname@example.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.
- 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