#include <iostream>
#include <regex>
#include <string>
#include <vector>
std::vector<std::string> splitWordsRegex(const std::string& input) {
std::regex re("\\W+"); // same meaning as Java: split on non‑word chars
std::sregex_token_iterator it(input.begin(), input.end(), re, -1);
std::sregex_token_iterator end;
std::vector<std::string> words;
for (; it != end; ++it) {
if (!it->str().empty()) {
std::string w = *it;
std::transform(w.begin(), w.end(), w.begin(),
[](unsigned char c){ return std::tolower(c); });
words.push_back(w);
}
}
return words;
}
std::string longestCommonPrefix(const std::string& input) {
auto words = splitWordsRegex(input);
if (words.empty()) return "";
std::string prefix = words[0];
for (size_t i = 1; i < words.size(); ++i) {
while (words[i].rfind(prefix, 0) != 0) { // prefix not at start
prefix.pop_back();
if (prefix.empty()) return "";
}
}
return prefix;
}
int main() {
std::string s1 = "The lowly inhabitants of the lowland were surprised to see the lower branches.";
std::cout << "LCP: '" << longestCommonPrefix(s1) << "'\n";
std::string s2 = "unclear, uncertain, unexpected";
std::cout << "LCP: '" << longestCommonPrefix(s2) << "'\n";
}
/*
run:
LCP: ''
LCP: 'un'
*/