0
Need you help for the following challange to improve it because it is too slow even it is correct!
Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2. Example 1: a1 = ["arp", "live", "strong"] a2 = ["lively", "alive", "harp", "sharp", "armstrong"] returns ["arp", "live", "strong"] Example 2: a1 = ["tarp", "mice", "bull"] a2 = ["lively", "alive", "harp", "sharp", "armstrong"] returns [] I think you can improve the code with using temporary variable and then sort substrings with swap() function inside the pocket. I sort it outside because of this it is too slow. https://code.sololearn.com/cw94BznenArJ/?ref=app
10 Réponses
0
Can you Tell me what this is for so in
can understand that Code?
0
Please look at examples, then you understand.
0
It might be a problem with your Phone
0
Because it running really fast at mine
0
Phone is not problem, problem arises when there are a lot of tests to check them if program is correct. I advice you if you know how to do it with temporary variable in the body of the code. And when you have current and previous variable, you sort them with swap() function. So you speed the code up.
0
K i try
0
I let chatgpt Run Over it
0
Maby this works
0
#include <iostream>
#include <vector>
#include <algorithm>
class WhichAreIn {
public:
static std::vector<std::string> inArray(const std::vector<std::string> &array1, const std::vector<std::string> &array2);
private:
std::vector<std::string> array1;
std::vector<std::string> array2;
};
std::vector<std::string> WhichAreIn::inArray(const std::vector<std::string> &array1, const std::vector<std::string> &array2) {
std::vector<std::string> result;
for (const std::string &s1 : array1) {
for (const std::string &s2 : array2) {
if (s2.find(s1) != std::string::npos) {
result.push_back(s1);
break; // No need to continue searching in this string
}
}
}
std::sort(result.begin(), result.end()); // Sort the result vector
// Remove duplicates from the result vector
result.erase(std::unique(result.begin(), result.end()), result.end());
return result;
}
int main() {
std::vector<std::string> array1 = {"1295
0
int main() {
std::vector<std::string> array1 = {"1295", "code", "1346", "1028", "ar"};
std::vector<std::string> array2 = {"12951295", "ode", "46", "10281066", "par"};
std::vector<std::string> result = WhichAreIn::inArray(array1, array2);
for (const std::string &s : result) {
std::cout << s << std::endl;
}
return 0;
}