Hi, I need help with the calculation of the temporary complexity of this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hi, I need help with the calculation of the temporary complexity of this code

The following java function receives as input two strings text and pattern, and returns the first text position where pattern is found. In case the pattern is not found, it returns -1. Calculate the time complexity of the indexOf function. Note that the execution time depends on both m and n. void indexOf(String text, String pattern) { int n = text.length(); int m = pattern.length(); for (int i = 0; i <= n - m; i++) { int j; for (j = 0; j < m; j++) if(text.charAt(i + j) != pattern.charAt(j)) break; if (j == m) return i; } return -1; }

13th Sep 2021, 5:24 AM
Juan pablo Peña
Juan pablo Peña - avatar
1 Answer
0
It looks, your code lines always return -1. The return -1 statement isn't connected any condition and it always works. So, you should use a condition and connect the return statements. I hope, it helps. Happy coding!
18th Sep 2021, 7:34 PM
mesarthim
mesarthim - avatar