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

# 14. Longest Common Prefix

![](/files/-MeXv6GOAxQjUnDRnVs7)

time: O(s), all the chars

space: O(1)

```java
class Solution {
    public String longestCommonPrefix(String[] strs) {
        // [abc, cds, ab] => ""
        // [abc, ab, a] => "a"
        // [aaa, bbb, cc] = > ""
        // fiist is to compare each char in string array one by one, it will be n^2
        
        // another one is base on first String, compare others and narrow the String range,
        if (strs == null || strs.length == 0) return "";
        
        String pre = strs[0];
        for (int i = 1; i < strs.length; i++) {
            while (!strs[i].startsWith(pre)) {
                pre = pre.substring(0, pre.length() - 1);
            }
        }
        return pre;
     }
}
```

## like binary search idea, separate to two parts

time: O(slogn), s: all the chars

space: O(1)

```java
class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) return "";
        
        int minLength = Integer.MAX_VALUE;
        for (String str : strs) {
            minLength = Math.min(minLength, str.length());
        }
        int low = 0; // it's should be 1
        int high = minLength;
        while (low <= high) {
            int mid = (low + high) >> 1;
            if (isCommon(strs, mid)) { // isCommon, so it can increase the common length
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return strs[0].substring(0, (low+high)/2);
    }
    private boolean isCommon(String[] strs, int index) {
        String pre = strs[0].substring(0, index);
        for (int i = 1; i < strs.length; i++) {
            if (!strs[i].startsWith(pre)) {
                return false;
            }       
        }
        return true;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/string/14.-longest-common-prefix.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
