You are given a string of digits panel and an array of strings codes. Each string in the…
- User
You are given a string of digits panel and an array of strings codes. Each string in the codes array consists of digits only and represents a code in the following format: <index», where both index and pattern should consist of at least one digit. Since there are several ways to split the code, let's consider them all in ascending order of index length and call them split-cases. For instance, for the code = "1324* , the split-cases are: split-case 1: index = "L" and pattern = "324" split-case 2: index = "13" and pattern = "24" split-case 3: index = 132" and pattern = "4" For each code in codes and for every split-case of this code, check whether a string pattern is present at the index position in the panel string. Return a string array consisting of results of these checks, where each element is either pattern, if this pattern is present in panel, or otherwise "not found
Example
For panel = 2311453915" and codes = ["0211, *639"], the output should be solution (panel, codes) = ["not Found",*11", "not found", *39". not found" - Explanation: Let's consider all possible codes: For codes [0] = *0211" there are three split-cases: index is 0 and pattern is 211. Since panel [0..2] = "231" = pattern, this is not a valid code. The answer for this split-case is "not found" • index is 02 and pattern is 11" • panel [2..3] = "11 = pattern, so this is a valid code. The answer for this split-case is For codes [1] = *639" there are two split-cases: index IS 6 and pattern is "39" . pane][6..7]="39" = pattern, so this is a valid code. The answer for this split-case is "39" index is 63 and pattern is 9- As panel[63] doesn't exist, this is not a valid code. The answer for this split-case is "not found" The resulting array is ['"not found", "11", "not found",
Answer this in c++ and ensure that the end user is able to run the test cases
- Assistant
Here's a C++ program that solves the problem as specified. The code includes a
solutionfunction that takes thepanelstring and thecodesvector of strings as input and returns a vector of strings containing the results for each split-case. There's also amainfunction that reads input from the user and prints the outputs.#include <iostream> #include <vector> #include <string> using namespace std; vector<string> solution(string panel, vector<string> codes) { vector<string> result; for (const string& code : codes) { int code_len = code.size(); // For each split-case for (int k = 1; k < code_len; ++k) { string index_str = code.substr(0, k); string pattern = code.substr(k); // Convert index_str to integer idx int idx; try { idx = stoi(index_str); } catch (...) { result.push_back("not found"); continue; } // Check if idx is within valid range if (idx < 0 || idx + pattern.size() > panel.size()) { result.push_back("not found"); continue; } // Compare the pattern with the substring at position idx if (panel.substr(idx, pattern.size()) == pattern) { result.push_back(pattern); } else { result.push_back("not found"); } } } return result; } int main() { string panel; cout << "Enter the panel string: "; cin >> panel; int n; cout << "Enter the number of codes: "; cin >> n; vector<string> codes(n); cout << "Enter the codes:" << endl; for (int i = 0; i < n; ++i) { cin >> codes[i]; } vector<string> outputs = solution(panel, codes); cout << "Outputs:" << endl; for (const string& output : outputs) { cout << output << endl; } return 0; }To run this program, you can compile it using a C++ compiler like
g++:g++ -o program program.cppThen run the executable:
./programSample Input:
Enter the panel string: 2311453915 Enter the number of codes: 2 Enter the codes: 0211 639Sample Output:
Outputs: not found 11 not found 39 not foundExplanation:
-
For the code "0211":
- Split-case 1: index = "0", pattern = "211"
- panel[0..2] = "231" ≠ "211" → "not found"
- Split-case 2: index = "02" (which is 2), pattern = "11"
- panel[2..3] = "11" = "11" → "11"
- Split-case 3: index = "021" (which is 21), pattern = "1"
- index out of bounds → "not found"
- Split-case 1: index = "0", pattern = "211"
-
For the code "639":
- Split-case 1: index = "6", pattern = "39"
- panel[6..7] = "39" = "39" → "39"
- Split-case 2: index = "63", pattern = "9"
- index out of bounds → "not found"
- Split-case 1: index = "6", pattern = "39"
Note: The program reads the input from the user, processes each code according to the specified rules, and outputs the results for each split-case in order.
-