Untitled
unknown
c_cpp
a year ago
1.2 kB
2
Indexable
Never
string getLongestRegex(string a, string b, string c) { // Initialize a set to store all possible characters in the regex. set<char> chars; for (char c : a) { chars.insert(c); } for (char c : b) { chars.insert(c); } // Initialize a stack to store the characters in the regex. stack<char> s; // Iterate over all characters in the alphabet. for (char c = 'A'; c <= 'Z'; c++) { // If the character is in the set, push it onto the stack. if (chars.count(c)) { s.push(c); } // Otherwise, try to construct a regex that matches a and b but not c. else { // Create a string to store the regex. string regex = "["; // Iterate over the characters on the stack. while (!s.empty()) { regex += s.top(); s.pop(); } // Add the character c to the regex. regex += c; // Add "]" to the regex. regex += "]"; // Check if the regex matches a and b. if (regex.find(c) == string::npos && regex.find(a) != string::npos && regex.find(b) != string::npos) { // The regex matches a and b but not c, so return it. return regex; } } } // No regex was found, so return -1. return "-1"; }