반응형
https://leetcode.com/problems/longest-common-prefix/
Longest Common Prefix - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
class Solution {
fun longestCommonPrefix(strs: Array<String>): String {
if (strs.size == 1) return strs.first()
var answer = ""
val sortedStrs = strs.sortedBy { it.length }
val firstStr = sortedStrs.first()
for (i: Int in firstStr.indices) {
val currentChar = firstStr[i]
if (sortedStrs.all { it[i] == currentChar }) {
answer += currentChar
} else {
return answer
}
}
return answer
}
}
https://github.com/960813/leetcode-problems/commit/8d164325ede946d3cc9bcf7400a20c98976e780b
[014] Kotlin · 960813/leetcode-problems@8d16432
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
github.com
반응형
☕️ Networking
기술 직군의 기술적인 교류, 커리어 이야기, 직군 무관 네트워킹 모두 환영합니다!
위클리 아카데미 오픈 채팅방(비밀번호: 9323)
kakaotalk: https://open.kakao.com/o/gyvuT5Yd