Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
String match with test
#include <iostream> #include <string> #include <tuple> #include <vector> using namespace std; bool stringMatch(const string& s1, const string& s2, int error, int pos1 = 0, int pos2 = 0) { if (error < 0) return false; int l1 = s1.length(); int l2 = s2.length(); if (pos1 >= l1) if (l2 - pos2 > error) return false; else return true; if (pos2 >= l2) if (l1 - pos1 > error) return false; else return true; while (pos1 < l1 && pos2 < l2) { if (s1[pos1] != s2[pos2]) return (stringMatch(s1, s2, error - 1, pos1 + 1, pos2 + 1) || stringMatch(s1, s2, error - 1, pos1, pos2 + 1) || stringMatch(s1, s2, error - 1, pos1 + 1, pos2)); ++pos1; ++pos2; } return stringMatch(s1, s2, error, pos1, pos2); } typedef tuple<string, string, int, bool> testType; bool validateTest(testType& testCase) { string& str1 = get<0>(testCase); string& str2 = get<1>(testCase); int error = get<2>(testCase); int expectedOut = get<3>(testCase); return (stringMatch(str1, str2, error) == expectedOut); } void test() { vector<testType> testCases; testCases.push_back(testType("aa","bb", 2, true)); testCases.push_back(testType("","", 0, true)); testCases.push_back(testType("a","", 1, true)); testCases.push_back(testType("hello","bello", 1, true)); testCases.push_back(testType("hello","ello", 1, true)); testCases.push_back(testType("hello","llo", 1, false)); testCases.push_back(testType("amazon","amzon", 1, true)); testCases.push_back(testType("amazon","ama", 3, true)); for (int i = 0; i < testCases.size(); ++i) { if (!validateTest(testCases[i])) cout << "Test " << i << ". failed!" << endl; } } int main() { test(); //cout << boolalpha << stringMatch("hellohello", "helloelloj", 2) << " - true" << endl; } /* bool stringMatch(const string& s1, const string& s2, int maxError = 2) { int l1 = s1.length(); int l2 = s2.length(); int length = l1 + l2; int diff = abs(l1 - l2); int errorPercent = floor(diff / (length / 100.0)); if (errorPercent > maxErrorPercent / 2) return false; int maxError = round((length / 100.0) * maxErrorPercent / 2); return stringMatchHelper(s1, s2, 0, 0, maxError); } */
run
|
edit
|
history
|
help
0
LRU - Set Sol
Trapping rain water problem
Shortest path in binary tree
lab17feb22x4B.cpp
Q
шаблонизированное наследование
typecasting
Policy based smart pointer
Correlation Coefficient
SEGMENTED SIEVE