Need you help for the following challange to improve it because it is too slow even it is correct! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

26th Sep 2023, 3:07 PM
TeaserCode
10 Answers
0
Can you Tell me what this is for so in can understand that Code?
28th Sep 2023, 1:41 PM
D1M3
D1M3 - avatar
0
Please look at examples, then you understand.
28th Sep 2023, 3:10 PM
TeaserCode
0
It might be a problem with your Phone
28th Sep 2023, 5:41 PM
D1M3
D1M3 - avatar
0
Because it running really fast at mine
28th Sep 2023, 5:42 PM
D1M3
D1M3 - avatar
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.
28th Sep 2023, 6:37 PM
TeaserCode
0
K i try
28th Sep 2023, 6:38 PM
D1M3
D1M3 - avatar
0
I let chatgpt Run Over it
28th Sep 2023, 6:42 PM
D1M3
D1M3 - avatar
0
Maby this works
28th Sep 2023, 6:42 PM
D1M3
D1M3 - avatar
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
28th Sep 2023, 6:42 PM
D1M3
D1M3 - avatar
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; }
28th Sep 2023, 6:43 PM
D1M3
D1M3 - avatar