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; }
1 Réponse
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!