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
#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;
}
|
Java
import java.util.*;
class GFG {
public static int countWays(String str, int n)
{
int len = str.length();
int count = 0 ;
for ( int i = 0 ; i <= len - n; i++) {
String s1 = str.substring(i, i + n);
for ( int j = i + 1 ; j <= len - n; j++) {
String s2 = str.substring(j, j + n);
int diff = 0 ;
for ( int k = 0 ; k < n; k++) {
if (s1.charAt(k) != s2.charAt(k)) {
diff++;
}
}
if (diff == 1 ) {
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = "abcb" ;
int N = 2 ;
System.out.println(countWays(str, N));
}
}
|
Python
def countWays( str , n):
len_ = len ( str )
count = 0
for i in range (len_ - n + 1 ):
s1 = str [i:i + n]
for j in range (i + 1 , len_ - n + 1 ):
s2 = str [j:j + n]
diff = 0
for k in range (n):
if s1[k] ! = s2[k]:
diff + = 1
if diff = = 1 :
count + = 1
return count
str_ = "abcb"
N = 2
print (countWays(str_, N))
|
C#
using System;
public class GFG {
static int CountWays( string str, int n)
{
int len = str.Length;
int count = 0;
for ( int i = 0; i <= len - n; i++) {
string s1 = str.Substring(i, n);
for ( int j = i + 1; j <= len - n; j++) {
string s2 = str.Substring(j, n);
int diff = 0;
for ( int k = 0; k < n; k++) {
if (s1[k] != s2[k])
diff++;
}
if (diff == 1)
count++;
}
}
return count;
}
static void Main( string [] args)
{
string str = "abcb" ;
int N = 2;
Console.WriteLine(CountWays(str, N));
}
}
|
Javascript
function countWays( str, n)
{
let len = str.length;
let count = 0;
for (let i = 0; i <= len - n; i++) {
let s1 = str.substr(i);
for (let j = i + 1; j <= len - n; j++) {
let s2 = str.substr(j);
let diff = 0;
for (let k = 0; k < n; k++)
if (s1[k] != s2[k])
diff++;
if (diff == 1)
count++;
}
}
return count;
}
let str = "abcb" ;
let N = 2;
console.log(countWays(str, N));
|
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)
Another Approach:
- The main function initializes a string “str” with the value “abcb” and an integer “N” with the value 2.
- The main function calls the “countWays” function with “str” and “N” as parameters.
- The “countWays” function first calculates the length of the input string “str”.
- The function initializes a variable “count” to store the number of ways to form substrings and a variable “diff” to store the count of different characters in the current substring.
- The function then initializes the first substring “s1” with the first “N” characters of the input string “str”.
- The function then counts the different characters in the first substring “s1” by comparing each character with the corresponding character “N” positions to the right of it in the input string “str”. If the characters are different, the “diff” variable is incremented.
- If the “diff” variable is equal to 1, it means that there is only one different character in the first substring, so the “count” variable is incremented.
- The function then slides the window from left to right by iterating through the input string “str” from the (N+1)th position to the end of the string.
- In each iteration, the function removes the leftmost character from the previous substring “s1” and adds the rightmost character from the new substring “s1”. The “diff” variable is updated accordingly.
- If the “diff” variable is equal to 1, it means that there is only one different character in the current substring, so the “count” variable is incremented.
- After iterating through all possible substrings, the function returns the “count” variable.
- Finally, the main function prints the value returned by the “countWays” function, which in this case is 2. This means that there are two ways to form substrings of length 2 from the input string “abcb” such that there is only one different character in each substring. The two possible substrings are “ab” and “bc”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countWays(string str, int N) {
int len = str.length();
int count = 0;
int diff = 0;
string s1 = str.substr(0, N);
for ( int i = 0; i < N; i++) {
if (s1[i] != str[i + N]) {
diff++;
}
}
if (diff == 1) {
count++;
}
for ( int i = 1; i <= len - N; i++) {
if (s1[i - 1] != str[i - 1]) {
diff--;
}
if (s1[N - 1] != str[i + N - 1]) {
diff++;
}
s1 = str.substr(i, N);
if (diff == 1) {
count++;
}
}
return count;
}
int main() {
string str = "abcb" ;
int N = 2;
cout << countWays(str, N);
return 0;
}
|
Java
import java.util.*;
class Main {
public static int countWays(String str, int N) {
int len = str.length();
int count = 0 ;
int diff = 0 ;
String s1 = str.substring( 0 , N);
for ( int i = 0 ; i < N; i++) {
if (s1.charAt(i) != str.charAt(i + N)) {
diff++;
}
}
if (diff == 1 ) {
count++;
}
for ( int i = 1 ; i <= len - N; i++) {
if (s1.charAt(i - 1 ) != str.charAt(i - 1 )) {
diff--;
}
if (s1.charAt(N - 1 ) != str.charAt(i + N - 1 )) {
diff++;
}
s1 = str.substring(i, i + N);
if (diff == 1 ) {
count++;
}
}
return count;
}
public static void main(String[] args) {
String str = "abcb" ;
int N = 2 ;
System.out.println(countWays(str, N));
}
}
|
Python3
def countWays( str , N):
le = len ( str )
count = 0
diff = 0
s1 = str [ 0 :N]
for i in range (N):
if s1[i] ! = str [i + N]:
diff + = 1
if diff = = 1 :
count + = 1
for i in range ( 1 , le - N + 1 ):
if s1[i - 1 ] ! = str [i - 1 ]:
diff - = 1
if s1[N - 1 ] ! = str [i + N - 1 ]:
diff + = 1
s1 = str [i:i + N]
if diff = = 1 :
count + = 1
return count
str = "abcb"
N = 2
print (countWays( str , N))
|
C#
using System;
class GFG
{
static int CountWays( string str, int N)
{
int len = str.Length;
int count = 0;
int diff = 0;
string s1 = str.Substring(0, N);
for ( int i = 0; i < N; i++)
{
if (s1[i] != str[i + N])
{
diff++;
}
}
if (diff == 1)
{
count++;
}
for ( int i = 1; i <= len - N; i++)
{
if (s1[i - 1] != str[i - 1])
{
diff--;
}
if (s1[N - 1] != str[i + N - 1])
{
diff++;
}
s1 = str.Substring(i, N);
if (diff == 1)
{
count++;
}
}
return count;
}
static void Main( string [] args)
{
string str = "abcb" ;
int N = 2;
Console.WriteLine(CountWays(str, N));
}
}
|
Javascript
function countWays(str, N) {
let le = str.length;
let count = 0;
let diff = 0;
let s1 = str.substring(0, N);
for (let i = 0; i < N; i++) {
if (s1[i] !== str[i + N]) {
diff += 1;
}
}
if (diff === 1) {
count += 1;
}
for (let i = 1; i < le - N + 1; i++) {
if (s1[i - 1] !== str[i - 1]) {
diff -= 1;
}
if (s1[N - 1] !== str[i + N - 1]) {
diff += 1;
}
s1 = str.substring(i, i + N);
if (diff === 1) {
count += 1;
}
}
return count;
}
let str = "abcb" ;
let N = 1;
console.log(countWays(str, N));
|
Time complexity: O(len*N)
Auxiliary Space: O(n)
Related Articles:
Last Updated :
16 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...