From cc53adcb1f0035db08a7d9d5a9ab3a8cefcb0cf4 Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Thu, 31 May 2018 19:25:29 +0100 Subject: [PATCH] Squarepoint Capital --- .../a_bit_more_complex_wildcard_match.cpp | 121 ++++++++++++++++++ .../training/simple_wildcard_match.cpp | 1 + 2 files changed, 122 insertions(+) create mode 100644 puzzles/interviews/training/a_bit_more_complex_wildcard_match.cpp diff --git a/puzzles/interviews/training/a_bit_more_complex_wildcard_match.cpp b/puzzles/interviews/training/a_bit_more_complex_wildcard_match.cpp new file mode 100644 index 0000000..b22b618 --- /dev/null +++ b/puzzles/interviews/training/a_bit_more_complex_wildcard_match.cpp @@ -0,0 +1,121 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let b:argv="" +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +/* + * Complete the function below. + */ +bool match(const string& pattern, const string& string) { + + auto s = string.begin(); + auto p = pattern.begin(); + + const std::string::const_iterator empty; + auto prev_p = empty; + auto prev_s = s; + while (s != string.end()) { + while ( p != pattern.end() && *p == '*' ) { + p++; + prev_p = p; + prev_s = s; + } + if ( p == pattern.end() ) { + if ( prev_p == p ){ + return true; + } else if ( prev_p != empty ) { + p = prev_p; + s = ++prev_s; + } else { + return false; + } + } + else if ( *s == *p ){ + s++; + p++; + } + else if ( *p == '?' ){ + s++; + p++; + } + else if ( prev_p != empty ) { + p = prev_p; + s = ++prev_s; + } + else { + return false; + } + } + while ( *p == '*' ) { + p++; + } + return p == pattern.end(); +} + + +void test( const std::string& str, const std::string& pattern ){ + bool res = match(pattern, str); + std::cout << str << " - " << pattern << " -> " << res < seconds = end - begin;