-1

I am trying to solve a question which says: Write a function to find the longest common prefix string amongst an array of strings.

Input:

["baab","bacb","b","cbc"]

Inside while loop, if i use break it returns an empty string which is correct answer:

""

But if i use break, it returns:

"ba"

Below is my code:

class Solution {
    public String longestCommonPrefix(String[] strs) {
        String first=strs[0];
        
        for(int i=1;i<=strs.length-1;i++){
            char[] c1 = first.toCharArray();
            char[] c2 = strs[i].toCharArray();            
            int index=0;
            first="";
            int len=0;
            
            if(c1.length<c2.length)
                 len=c1.length;
                else
                     len=c2.length;
                
            if(len==0){
                return first;
            }
            while(index<=len-1){
                if(c1[index]==c2[index]){
                    first=first+c1[index];
                }else{
                    **return first;**
                }
                index++;
            }
        }
        return first;
    }
}

As per my understanding, when we return inside a while loop(which is the inner loop here), it should come out of that particular loop and not from the outer for loop, similar to break. Please help me understand why it is coming out of the for loop here.

user1326784
  • 627
  • 3
  • 11
  • 31
  • 2
    What does the debugger say? – justanotherguy Jun 17 '22 at 09:23
  • There's a good chance that only the first two words are getting checked, in which case, "ba" is the correct answer. – justanotherguy Jun 17 '22 at 09:23
  • 3
    `return` stops the function execution, and not just a loop – qrsngky Jun 17 '22 at 09:26
  • 2
    "As per my understanding, when we return inside a while loop(which is the inner loop here), it should come out of that particular loop and not from the outer for loop, similar to break." <- That is wrong. `return` returns from the current method. It doesn't care about if it's in a loop or even inside 100 nested loops. A return statement will always end the whole method execution and return from it. It is completely different than a break statement. – OH GOD SPIDERS Jun 17 '22 at 09:26
  • Loops don't even have return values like methods, hence returning from a loop doesn't even make sense logically. Returning is always done from methods. – OH GOD SPIDERS Jun 17 '22 at 09:29
  • return always exits the function, youre looking for break after setting a variable with scope outside the loop. – Matthew Ciaramitaro Jun 17 '22 at 09:52

1 Answers1

3

As per my understanding, when we return inside a while loop(which is the inner loop here), it should come out of that particular loop and not from the outer for loop, similar to break.

That is a wrong assumption: according to the docs, the method returns when it reaches the return statement:

A method returns to the code that invoked it when it

  • completes all the statements in the method,
  • reaches a return statement, or
  • throws an exception (covered later),

whichever occurs first.

To break out of inner loops, see this question: How do I break out of nested loops in Java?

TmTron
  • 17,012
  • 10
  • 94
  • 142