Count ways to select two N sized Substrings differing by one letter
Given a string str, the task is to find the number of ways two non-overlapping substrings of length N can be selected out of a string that differs by just one letter.
Examples:
Input: str = “abcd”, N = 1
Output: 6
Explanation: Possible non overlapping substrings pairs are (“a”, “b”), (“b”, “c”), (“c”, “d”), (“a”, “c”), (“a”, “d”) and (“b”, “d”).Input: str = “abcb”, N = 2
Output: 1
Explanation: The only possible substring pair is (“ab”, “cb”).
Naive Approach: To solve the problem follow the below idea:
The approach is to find the number of ways two substrings of a given string, “str”, of length “n” can differ by just one letter. The code first initializes a variable “count” to store the number of ways and a variable “len” to store the length of the given string. Then, the code uses two nested for loops to iterate through all possible substrings of length “n” within the given string. For each pair of substrings found, the code compares each letter of the two substrings. If the two substrings differ by just one letter, the code increases the count by 1. Finally, the code returns the count as the result.
Follow the steps of the code:
- Declare a variable “count” and initialize it with 0. This variable will be used to store the number of ways the substrings differ by just one letter.
- Use a for loop to iterate through all substrings of length “N” in the input string “str”. The loop starts from index 0 and goes up to the (length of the string – N)
- Within the for loop, use the substring method to store the first substring in a variable “s1”.
- Use another for loop to iterate through all substrings of length “N” starting from the (i + 1)th position.
- Within the second for loop, use the substring method to store the second substring in a variable “s2”.
- Declare a variable “diff” and initialize it with 0. This variable will be used to store the number of different letters between the two substrings.
- Use another for loop to iterate through the letters of the two substrings.
- Within the third for loop, compare each letter of the two substrings.
- If they are different, increment the “diff” variable by 1.
- Check if the value of “diff” is equal to 1, if so increase the “count” variable by 1.
- Exit the second for loop
- Exit the first for loop
- Return the value of “count”
Below is the implementation for the above approach:
C++14
|
Time Complexity: O((len – N)*(len – N) * N), where N is the required substring length and len is the length of string str.
Auxiliary Space: O(1)
Related Articles:
Given a string str, the task is to find the number of ways two non-overlapping substrings of length N can be selected out of a string that differs by just one letter.
Examples:
Input: str = “abcd”, N = 1
Output: 6
Explanation: Possible non overlapping substrings pairs are (“a”, “b”), (“b”, “c”), (“c”, “d”), (“a”, “c”), (“a”, “d”) and (“b”, “d”).Input: str = “abcb”, N = 2
Output: 1
Explanation: The only possible substring pair is (“ab”, “cb”).
Naive Approach: To solve the problem follow the below idea:
The approach is to find the number of ways two substrings of a given string, “str”, of length “n” can differ by just one letter. The code first initializes a variable “count” to store the number of ways and a variable “len” to store the length of the given string. Then, the code uses two nested for loops to iterate through all possible substrings of length “n” within the given string. For each pair of substrings found, the code compares each letter of the two substrings. If the two substrings differ by just one letter, the code increases the count by 1. Finally, the code returns the count as the result.
Follow the steps of the code:
- Declare a variable “count” and initialize it with 0. This variable will be used to store the number of ways the substrings differ by just one letter.
- Use a for loop to iterate through all substrings of length “N” in the input string “str”. The loop starts from index 0 and goes up to the (length of the string – N)
- Within the for loop, use the substring method to store the first substring in a variable “s1”.
- Use another for loop to iterate through all substrings of length “N” starting from the (i + 1)th position.
- Within the second for loop, use the substring method to store the second substring in a variable “s2”.
- Declare a variable “diff” and initialize it with 0. This variable will be used to store the number of different letters between the two substrings.
- Use another for loop to iterate through the letters of the two substrings.
- Within the third for loop, compare each letter of the two substrings.
- If they are different, increment the “diff” variable by 1.
- Check if the value of “diff” is equal to 1, if so increase the “count” variable by 1.
- Exit the second for loop
- Exit the first for loop
- Return the value of “count”
Below is the implementation for the above approach:
C++14
|
Time Complexity: O((len – N)*(len – N) * N), where N is the required substring length and len is the length of string str.
Auxiliary Space: O(1)
Related Articles: