
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to count substrings that differ by one character in Python
Suppose we have two strings s and t, we have to find the number of ways we can select a nonempty substring of s and replace one single character by another different character such that the resulting substring is one of the substring of t. We have to find the number of substrings that satisfy the condition above.
So, if the input is like s = "sts" t = "tsts", then the output will be 6 because the following are the pairs of substrings from s and t that differ by 1 character −
- ("sts", "tsts"),
- ("sts", "tsts"),
- ("sts", "tsts"),
- ("sts", "tsts"),
- ("sts", "tsts"),
- ("sts", "tsts")
The bold character portions are the substrings that are chosen from two strings s and t.
To solve this, we will follow these steps −
- n1 := size of s
- n2 := size of t
- ans := 0
- for each index i1 and character c1 from s, do
- for each index i2, and character c2 from t, do
- i := i1, j := i2
- while i < n1 and j < n2 and s[i] is same as t[j], do
- i := i + 1, j := j + 1
- if i < n1 and j < n2 and s[i] is not same as t[j], then
- := i + 1, j := j + 1
- ans := ans + 1
- while i < n1 and j < n2 and s[i] is same as t[j], do
- i := i + 1, j := j + 1
- ans := ans + 1
- for each index i2, and character c2 from t, do
- return ans
Example
Let us see the following implementation to get better understanding −
def solve(s, t): n1 = len(s) n2 = len(t) ans = 0 for i1, c1 in enumerate(s): for i2, c2 in enumerate(t): i = i1 j = i2 while i < n1 and j < n2 and s[i] == t[j]: i += 1 j += 1 if i < n1 and j < n2 and s[i] != t[j]: i += 1 j += 1 ans += 1 while i < n1 and j < n2 and s[i] == t[j]: i += 1 j += 1 ans += 1 return ans s = "sts" t = "tsts" print(solve(s, t))
Input
"sts", "tsts"
Output
6
- Related Articles
- Count substrings that starts with character X and ends with character Y in C++
- Program to count number of palindromic substrings in Python
- Program to count number of homogenous substrings in Python
- Program to count maximum score from removing substrings in Python
- Program to count number of distinct substrings in s in Python
- Program to find out if the strings supplied differ by a character in the same position in Python
- Program to find island count by adding blocks into grid one by one in Python
- Program to count substrings with all 1s in binary string in Python
- Program to count number of similar substrings for each query in Python
- Program to find the indexes where the substrings of a string match with another string fully or differ at one position in python
- Count substrings with each character occurring at most k times in C++
- Remove Substrings in One Iteration in python
- Program to count minimum deletions needed to make character frequencies unique in Python
- Python program to find the most occurring character and its count
- Divide a big number into two parts that differ by k in C++ Program

Advertisements