In: Computer Science
THE STRING MATCH PROBLEM
C++ only. Must use loops. Please do not use sequence of if statements. .
Given 2 strings, a and b, set result to the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings.
• for input of "xxcaazz", "xxbaaz" → 3
• for input of "abc", "abc" → 2
• for input of "abc", "axc" → 0
Here, we want to find the number of positions where they contain the same length 2 substrings. So, for this we follow the following algorithm:
Please refer to the comments of the program for more clarity.
#include<bits/stdc++.h>
using namespace std;
int countMatching(string a, string b) { // Function to find the number of matching sub-strings
int c = 0; // Initialized the counter by 0
for (int i = 0; i < a.length() - 1; i++) { // Loop for string a
for (int j = 0; j < b.length() - 1; j++) { // Loop for string b
string aSubString = a.substr(i, 2); // Finding substring of a for matching
string bSubString = b.substr(j, 2); // Finding the substring of b for matching
if (aSubString == bSubString) {
c++; // If matching then increase the counter by 1
}
}
}
return c; // Returning the answer
}
int main()
{
/*
Here we are taking string a and string b as input, you can also replace it with the initialized strings.
*/
string a,b;
cin >> a >> b;
int ans = countMatching(a, b); // Calling the function
cout << ans << endl; // Printing the result
return 0;
}
/*
Sample Input/Output:
Input:
xxcaazz
xxbaaz
Output:
3
*/
I am also attaching sample inputs and outputs for your reference.
Please let me know in the comments if you have any confusion. Also, please upvote if you like.