g0001_0100.s0014_longest_common_prefix.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0014_longest_common_prefix
// #Easy #Top_Interview_Questions #String #Level_2_Day_2_String #Udemy_Strings
// #2022_03_29_Time_269_ms_(48.51%)_Space_34.8_MB_(98.92%)
class Solution {
fun longestCommonPrefix(strs: Array): String {
if (strs.isEmpty()) {
return ""
}
if (strs.size == 1) {
return strs[0]
}
var temp = strs[0]
var i = 1
var cur: String
while (temp.length > 0 && i < strs.size) {
if (temp.length > strs[i].length) {
temp = temp.substring(0, strs[i].length)
}
cur = strs[i].substring(0, temp.length)
if (cur != temp) {
temp = temp.substring(0, temp.length - 1)
} else {
i++
}
}
return temp
}
}