https://leetcode.cn/problems/longest-common-prefix/?envType=study-plan-v2&envId=top-interview-150

package leetcode150

import (
    "testing"
)

func TestLongestCommonPrefix(t *testing.T) {
    str := []string{"flower", "flow", "flight"}
    res := longestCommonPrefix(str)
    println(res)
}

func longestCommonPrefix(strs []string) string {
    if len(strs) == 0 || len(strs[0]) == 0 {
        return ""
    }
    preSame := ""
    for i := 0; i < len(strs[0]); i++ {
        c := strs[0][i]
        for _, str := range strs {
            if len(str) == i || str[i] != c {
                return preSame
            }
        }
        preSame += string(c)
    }
    return preSame
}