How do I calculate the runtime complexity of this implementation of longest common prefix algorithm?
Below is my code:
/**
* @param string strs
* @return string
*/
var longestCommonPrefix = function(strs)
if(strs.length ===0) return '';
let prefix = strs[0];
for(let i=0; i< strs.length; i++)
while(strs[i].indexOf(prefix) !=0)
prefix = prefix.substring(0, prefix.length -1);
if(!prefix) return '';
return prefix;
;
Here for loop runs O(n) times and while loop within checks whether prefix matches the elements in for loop iteration - is the while loop O(n) times too? Please provide insight
algorithm runtime time-complexity
add a comment |
Below is my code:
/**
* @param string strs
* @return string
*/
var longestCommonPrefix = function(strs)
if(strs.length ===0) return '';
let prefix = strs[0];
for(let i=0; i< strs.length; i++)
while(strs[i].indexOf(prefix) !=0)
prefix = prefix.substring(0, prefix.length -1);
if(!prefix) return '';
return prefix;
;
Here for loop runs O(n) times and while loop within checks whether prefix matches the elements in for loop iteration - is the while loop O(n) times too? Please provide insight
algorithm runtime time-complexity
add a comment |
Below is my code:
/**
* @param string strs
* @return string
*/
var longestCommonPrefix = function(strs)
if(strs.length ===0) return '';
let prefix = strs[0];
for(let i=0; i< strs.length; i++)
while(strs[i].indexOf(prefix) !=0)
prefix = prefix.substring(0, prefix.length -1);
if(!prefix) return '';
return prefix;
;
Here for loop runs O(n) times and while loop within checks whether prefix matches the elements in for loop iteration - is the while loop O(n) times too? Please provide insight
algorithm runtime time-complexity
Below is my code:
/**
* @param string strs
* @return string
*/
var longestCommonPrefix = function(strs)
if(strs.length ===0) return '';
let prefix = strs[0];
for(let i=0; i< strs.length; i++)
while(strs[i].indexOf(prefix) !=0)
prefix = prefix.substring(0, prefix.length -1);
if(!prefix) return '';
return prefix;
;
Here for loop runs O(n) times and while loop within checks whether prefix matches the elements in for loop iteration - is the while loop O(n) times too? Please provide insight
algorithm runtime time-complexity
algorithm runtime time-complexity
asked Nov 15 '18 at 16:07
novemberskynovembersky
342614
342614
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Let n be the number of strings in strs
, and let the average (or longest) string length be m. Your algorithm with line numbers is as follows.
1. var longestCommonPrefix = function(strs)
2. if(strs.length ===0) return '';
3. let prefix = strs[0];
4. for(let i=0; i< strs.length; i++)
5. while(strs[i].indexOf(prefix) !=0)
6. prefix = prefix.substring(0, prefix.length - 1);
7. if(!prefix) return '';
8.
9.
10. return prefix;
11. ;
We only need estimate the number of times the for loop (line 4) and the while loop (line 5) are executed. As you noted that the for loop is executed n times, since n = strs.length
. However, you do have a return statement within the while loop, so execution might never reach line 10.
Now, for the while loop. As described, this loop is executed the size of the prefix (m) and in each iteration the indexOf operation takes O(m^2) in the worst case (see next paragraph). Thus, the time complexity of your algorithm as a function of the number of strings (n) is O(nm^2).
We could also look at the time complexity as a function of m, the number of characters in each string. How long does it take to compare two strings? Java's implementation in this case will take O(mk), where m and k are the lengths of the two strings respectively (see this answer to this question) and the String class code for details.
thanks a lot @rrufai but I got another doubt after reading one of the comments in the link shared above. Per Andreas, O(m^2) is worst case and least likely, cos pattern matching will stop as soon as first mismatch occurs and amortized, it is O(1.1 * n), which is O(n). So do we calculate the average case or the worst one?
– novembersky
Nov 16 '18 at 1:33
1
@novembersky: Usually, for big O analysis, we go with the worst-case. In fact, it's sometimes called worst-case analysis. I hope this helps.
– rrufai
Nov 16 '18 at 1:57
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323477%2fhow-do-i-calculate-the-runtime-complexity-of-this-implementation-of-longest-comm%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Let n be the number of strings in strs
, and let the average (or longest) string length be m. Your algorithm with line numbers is as follows.
1. var longestCommonPrefix = function(strs)
2. if(strs.length ===0) return '';
3. let prefix = strs[0];
4. for(let i=0; i< strs.length; i++)
5. while(strs[i].indexOf(prefix) !=0)
6. prefix = prefix.substring(0, prefix.length - 1);
7. if(!prefix) return '';
8.
9.
10. return prefix;
11. ;
We only need estimate the number of times the for loop (line 4) and the while loop (line 5) are executed. As you noted that the for loop is executed n times, since n = strs.length
. However, you do have a return statement within the while loop, so execution might never reach line 10.
Now, for the while loop. As described, this loop is executed the size of the prefix (m) and in each iteration the indexOf operation takes O(m^2) in the worst case (see next paragraph). Thus, the time complexity of your algorithm as a function of the number of strings (n) is O(nm^2).
We could also look at the time complexity as a function of m, the number of characters in each string. How long does it take to compare two strings? Java's implementation in this case will take O(mk), where m and k are the lengths of the two strings respectively (see this answer to this question) and the String class code for details.
thanks a lot @rrufai but I got another doubt after reading one of the comments in the link shared above. Per Andreas, O(m^2) is worst case and least likely, cos pattern matching will stop as soon as first mismatch occurs and amortized, it is O(1.1 * n), which is O(n). So do we calculate the average case or the worst one?
– novembersky
Nov 16 '18 at 1:33
1
@novembersky: Usually, for big O analysis, we go with the worst-case. In fact, it's sometimes called worst-case analysis. I hope this helps.
– rrufai
Nov 16 '18 at 1:57
add a comment |
Let n be the number of strings in strs
, and let the average (or longest) string length be m. Your algorithm with line numbers is as follows.
1. var longestCommonPrefix = function(strs)
2. if(strs.length ===0) return '';
3. let prefix = strs[0];
4. for(let i=0; i< strs.length; i++)
5. while(strs[i].indexOf(prefix) !=0)
6. prefix = prefix.substring(0, prefix.length - 1);
7. if(!prefix) return '';
8.
9.
10. return prefix;
11. ;
We only need estimate the number of times the for loop (line 4) and the while loop (line 5) are executed. As you noted that the for loop is executed n times, since n = strs.length
. However, you do have a return statement within the while loop, so execution might never reach line 10.
Now, for the while loop. As described, this loop is executed the size of the prefix (m) and in each iteration the indexOf operation takes O(m^2) in the worst case (see next paragraph). Thus, the time complexity of your algorithm as a function of the number of strings (n) is O(nm^2).
We could also look at the time complexity as a function of m, the number of characters in each string. How long does it take to compare two strings? Java's implementation in this case will take O(mk), where m and k are the lengths of the two strings respectively (see this answer to this question) and the String class code for details.
thanks a lot @rrufai but I got another doubt after reading one of the comments in the link shared above. Per Andreas, O(m^2) is worst case and least likely, cos pattern matching will stop as soon as first mismatch occurs and amortized, it is O(1.1 * n), which is O(n). So do we calculate the average case or the worst one?
– novembersky
Nov 16 '18 at 1:33
1
@novembersky: Usually, for big O analysis, we go with the worst-case. In fact, it's sometimes called worst-case analysis. I hope this helps.
– rrufai
Nov 16 '18 at 1:57
add a comment |
Let n be the number of strings in strs
, and let the average (or longest) string length be m. Your algorithm with line numbers is as follows.
1. var longestCommonPrefix = function(strs)
2. if(strs.length ===0) return '';
3. let prefix = strs[0];
4. for(let i=0; i< strs.length; i++)
5. while(strs[i].indexOf(prefix) !=0)
6. prefix = prefix.substring(0, prefix.length - 1);
7. if(!prefix) return '';
8.
9.
10. return prefix;
11. ;
We only need estimate the number of times the for loop (line 4) and the while loop (line 5) are executed. As you noted that the for loop is executed n times, since n = strs.length
. However, you do have a return statement within the while loop, so execution might never reach line 10.
Now, for the while loop. As described, this loop is executed the size of the prefix (m) and in each iteration the indexOf operation takes O(m^2) in the worst case (see next paragraph). Thus, the time complexity of your algorithm as a function of the number of strings (n) is O(nm^2).
We could also look at the time complexity as a function of m, the number of characters in each string. How long does it take to compare two strings? Java's implementation in this case will take O(mk), where m and k are the lengths of the two strings respectively (see this answer to this question) and the String class code for details.
Let n be the number of strings in strs
, and let the average (or longest) string length be m. Your algorithm with line numbers is as follows.
1. var longestCommonPrefix = function(strs)
2. if(strs.length ===0) return '';
3. let prefix = strs[0];
4. for(let i=0; i< strs.length; i++)
5. while(strs[i].indexOf(prefix) !=0)
6. prefix = prefix.substring(0, prefix.length - 1);
7. if(!prefix) return '';
8.
9.
10. return prefix;
11. ;
We only need estimate the number of times the for loop (line 4) and the while loop (line 5) are executed. As you noted that the for loop is executed n times, since n = strs.length
. However, you do have a return statement within the while loop, so execution might never reach line 10.
Now, for the while loop. As described, this loop is executed the size of the prefix (m) and in each iteration the indexOf operation takes O(m^2) in the worst case (see next paragraph). Thus, the time complexity of your algorithm as a function of the number of strings (n) is O(nm^2).
We could also look at the time complexity as a function of m, the number of characters in each string. How long does it take to compare two strings? Java's implementation in this case will take O(mk), where m and k are the lengths of the two strings respectively (see this answer to this question) and the String class code for details.
edited Nov 15 '18 at 22:21
answered Nov 15 '18 at 22:10
rrufairrufai
1,3551125
1,3551125
thanks a lot @rrufai but I got another doubt after reading one of the comments in the link shared above. Per Andreas, O(m^2) is worst case and least likely, cos pattern matching will stop as soon as first mismatch occurs and amortized, it is O(1.1 * n), which is O(n). So do we calculate the average case or the worst one?
– novembersky
Nov 16 '18 at 1:33
1
@novembersky: Usually, for big O analysis, we go with the worst-case. In fact, it's sometimes called worst-case analysis. I hope this helps.
– rrufai
Nov 16 '18 at 1:57
add a comment |
thanks a lot @rrufai but I got another doubt after reading one of the comments in the link shared above. Per Andreas, O(m^2) is worst case and least likely, cos pattern matching will stop as soon as first mismatch occurs and amortized, it is O(1.1 * n), which is O(n). So do we calculate the average case or the worst one?
– novembersky
Nov 16 '18 at 1:33
1
@novembersky: Usually, for big O analysis, we go with the worst-case. In fact, it's sometimes called worst-case analysis. I hope this helps.
– rrufai
Nov 16 '18 at 1:57
thanks a lot @rrufai but I got another doubt after reading one of the comments in the link shared above. Per Andreas, O(m^2) is worst case and least likely, cos pattern matching will stop as soon as first mismatch occurs and amortized, it is O(1.1 * n), which is O(n). So do we calculate the average case or the worst one?
– novembersky
Nov 16 '18 at 1:33
thanks a lot @rrufai but I got another doubt after reading one of the comments in the link shared above. Per Andreas, O(m^2) is worst case and least likely, cos pattern matching will stop as soon as first mismatch occurs and amortized, it is O(1.1 * n), which is O(n). So do we calculate the average case or the worst one?
– novembersky
Nov 16 '18 at 1:33
1
1
@novembersky: Usually, for big O analysis, we go with the worst-case. In fact, it's sometimes called worst-case analysis. I hope this helps.
– rrufai
Nov 16 '18 at 1:57
@novembersky: Usually, for big O analysis, we go with the worst-case. In fact, it's sometimes called worst-case analysis. I hope this helps.
– rrufai
Nov 16 '18 at 1:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323477%2fhow-do-i-calculate-the-runtime-complexity-of-this-implementation-of-longest-comm%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown