How to improve the time complexity of this code?
Hello! I'm trying to solve this problem, basically I want to expand the strings given some rules: * Each alphabet letter is mapped like this: A = 0, B = 1, C = 2, D = 3, ..., Z = 25 . * I should get a letter of the alphabet by using the next: (code(x ) + code(y )) mod 26, where "x" and "y" are two letters, the result of this will give me the position of the letter. I take as input a string and how many times I want to expand that string, for example: First example: * Input: "ACB" 3 (Here my original string is "ACB" and I want to expand it 3 times given the rules above) * Expected string expanded should be: "ACCECGEGCHFIDHEFB" (this is the result) Second Example: * input: AN 5 * Result: ANNANNANNANNANNANNANNANNANNANNANN Third example: * input: CC 4 * result: CKIOGQKOEOKQGOIKC I understand how I should expand the string and I have already the code that does that, here is the piece of code: https://www.sololearn.com/compiler-playground/cDxiqBvHH8BT My problem is, that when I upload the code to an online judge, I get "TIME LIMIT", to be honest I don't know how to make more efficient the code to avoid "TIME LIMIT" from the judge, my original solution was to use the function string "insert" to insert the proper character at a given index in the string, but I got TIME LIMIT, so I thought that maybe not using the "insert" string function and insert the character manually could be a solution, but I also get TIME LIMIT Code: while (iter < n) { hash = code(current_word[i]) + code(current_word[i+1]); int position = (hash) % 26; string c = string(1, 'A' + position); string previous_word = current_word; current_word.insert(i + 1, c); i += 2; if (i >= previous_word.size()) { iter++; i = 0; } } Does anyone know how I can improve the code? is O(n*m) where "n" is the number of iterations and "m" the string length