Count substrings of same length differing by a single character from two given strings
Last Updated :
01 Feb, 2022
Given two strings S and T of length N and M respectively, the task is to count the number of ways of obtaining same-length substring from both the strings such that they have a single different character.
Examples:
Input: S = “ab”, T = “bb”
Output: 3
Explanation: The following are the pairs of substrings from S and T differ by a single character:
- (“a”, “b”)
- (“a”, “b”)
- (“ab”, “bb”)
Input: S = “aba”, T = “baba”
Output: 6
Naive Approach: The simplest approach is to generate all possible substrings from both the given strings and then count all possible pairs of substrings of the same lengths which can be made equal by changing a single character.
Time Complexity: O(N3*M3)
Auxiliary Space: O(N2)
Efficient Approach: To optimize the above approach, the idea is to iterate over all characters of both the given strings simultaneously and for each pair of different characters, count all those substrings of equal length starting from the next index of the current different character. Print the count obtained after checking for all pairs of different characters.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubstrings(string s, string t)
{
int answ = 0;
for ( int i = 0; i < s.size(); i++)
{
for ( int j = 0; j < t.size(); j++)
{
if (t[j] != s[i])
{
answ += 1;
int k = 1;
int z = -1;
int q = 1;
while (j + z >= 0 &&
0 <= i + z &&
s[i + z] ==
t[j + z])
{
z -= 1;
answ += 1;
q += 1;
}
while (s.size() > i + k &&
j + k < t.size() &&
s[i + k] ==
t[j + k])
{
k += 1;
answ += q;
z = -1;
}
}
}
}
return answ;
}
int main()
{
string S = "aba" ;
string T = "baba" ;
cout<<(countSubstrings(S, T));
}
|
Java
class GFG{
static int countSubStrings(String s, String t)
{
int answ = 0 ;
for ( int i = 0 ; i < s.length(); i++)
{
for ( int j = 0 ; j < t.length(); j++)
{
if (t.charAt(j) != s.charAt(i))
{
answ += 1 ;
int k = 1 ;
int z = - 1 ;
int q = 1 ;
while (j + z >= 0 &&
0 <= i + z &&
s.charAt(i + z) ==
t.charAt(j + z))
{
z -= 1 ;
answ += 1 ;
q += 1 ;
}
while (s.length() > i + k &&
j + k < t.length() &&
s.charAt(i + k) ==
t.charAt(j + k))
{
k += 1 ;
answ += q;
z = - 1 ;
}
}
}
}
return answ;
}
public static void main(String[] args)
{
String S = "aba" ;
String T = "baba" ;
System.out.println(countSubStrings(S, T));
}
}
|
Python3
def countSubstrings(s, t):
answ = 0
for i in range ( len (s)):
for j in range ( len (t)):
if t[j] ! = s[i]:
answ + = 1
k = 1
z = - 1
q = 1
while (
j + z > = 0 < = i + z and
s[i + z] = = t[j + z]
):
z - = 1
answ + = 1
q + = 1
while (
len (s) > i + k and
j + k < len (t) and
s[i + k] = = t[j + k]
):
k + = 1
answ + = q
z = - 1
return answ
S = "aba"
T = "baba"
print (countSubstrings(S, T))
|
C#
using System;
class GFG
{
static int countSubStrings(String s, String t)
{
int answ = 0;
for ( int i = 0; i < s.Length; i++)
{
for ( int j = 0; j < t.Length; j++)
{
if (t[j] != s[i])
{
answ += 1;
int k = 1;
int z = -1;
int q = 1;
while (j + z >= 0 &&
0 <= i + z &&
s[i + z] ==
t[j + z])
{
z -= 1;
answ += 1;
q += 1;
}
while (s.Length > i + k &&
j + k < t.Length &&
s[i + k] ==
t[j + k])
{
k += 1;
answ += q;
z = -1;
}
}
}
}
return answ;
}
public static void Main(String[] args)
{
String S = "aba" ;
String T = "baba" ;
Console.WriteLine(countSubStrings(S, T));
}
}
|
Javascript
<script>
function countSubStrings(s, t)
{
let answ = 0;
for (let i = 0; i < s.length; i++)
{
for (let j = 0; j < t.length; j++)
{
if (t[j] != s[i])
{
answ += 1;
let k = 1;
let z = -1;
let q = 1;
while (j + z >= 0 &&
0 <= i + z &&
s[i + z] ==
t[j + z])
{
z -= 1;
answ += 1;
q += 1;
}
while (s.length > i + k &&
j + k < t.length &&
s[i + k] ==
t[j + k])
{
k += 1;
answ += q;
z = -1;
}
}
}
}
return answ;
}
let S = "aba" ;
let T = "baba" ;
document.write(countSubStrings(S, T));
</script>
|
Time Complexity: O(N*M*max(N,M))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...