Techno Blender
Digitally Yours.

Count ways to select two N sized Substrings differing by one letter

0 62


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

#include <bits/stdc++.h>

using namespace std;

  

int countWays(string str, int n)

{

    int len = str.length();

  

    

    int count = 0;

  

    

    

    for (int i = 0; i <= len - n; i++) {

  

        

        string s1 = str.substr(i, n);

  

        

        

        

        for (int j = i + 1; j <= len - n; j++) {

  

            

            string s2 = str.substr(j, n);

  

            

            

            int diff = 0;

            for (int k = 0; k < n; k++)

                if (s1[k] != s2[k])

                    diff++;

  

            

            

            

            if (diff == 1)

                count++;

        }

    }

  

    return count;

}

  

int main()

{

    string str = "abcb";

    int N = 2;

  

    

    cout << countWays(str, N);

    return 0;

}

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

#include <bits/stdc++.h>

using namespace std;

  

int countWays(string str, int n)

{

    int len = str.length();

  

    

    int count = 0;

  

    

    

    for (int i = 0; i <= len - n; i++) {

  

        

        string s1 = str.substr(i, n);

  

        

        

        

        for (int j = i + 1; j <= len - n; j++) {

  

            

            string s2 = str.substr(j, n);

  

            

            

            int diff = 0;

            for (int k = 0; k < n; k++)

                if (s1[k] != s2[k])

                    diff++;

  

            

            

            

            if (diff == 1)

                count++;

        }

    }

  

    return count;

}

  

int main()

{

    string str = "abcb";

    int N = 2;

  

    

    cout << countWays(str, N);

    return 0;

}

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:

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment