> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/01-23-2022-14.md).

# 01/23/2022 14

vertical scanning. scan the char from the beginning to the end of each string in the array and compare it one by one. If they are different or the index is larger than one of the string's length, then return the substring of the strs\[0] from 0 to index i.

Time O(n \* m) m is the minimal length of the strings in the array.&#x20;

Space O(1)

class Solution {&#x20;

public String longestCommonPrefix(String\[] strs) {&#x20;

if (strs.length == 0) return "";

```
for (int i = 0; i < strs[0].length(); i++){
    char check = strs[0].charAt(i);
    
    for (int j = 1; j < strs.length; j++){
        if(i >= strs[j].length() || check != strs[j].charAt(i)){
            return strs[0].substring(0,i);
        }
    }
}

    return strs[0];
}
```

}
