a simple mistake every beginner usually do.

Revision en1, by ethico, 2019-09-17 10:25:05

usually when i started coding and started learning about string array or normal array ,i used to work with string array or array by typing strlen() or size() in the for loop condition.

for(int i=0;i<strlen();i++)
{
    statement;
}

but its a totally wrong way to set up the condition.because normally if my algorithms complexity is O(n),and i use strlen() it,my time complexity will become O(n^2). **** this happens because every time for loop come to check the condition it will start checking what is the string length is,so total complexity will become n*n = n^2 **** so to avoid this kind of situation ,declare integer variable n= strlen(),and then put the code like this

~~~~~ int n= strlen(); for(int i=0;i<n;i++) { statement; }~~~~~

i hope this will be helpful :).

Tags #complexity, #strings

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English ethico 2019-09-17 10:25:51 2 Tiny change: 'tement;\n}~~~~~\n\n*' -> 'tement;\n}\n~~~~~\n\n*'
en1 English ethico 2019-09-17 10:25:05 879 Initial revision (published)