mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
19 lines
223 B
Go
19 lines
223 B
Go
package leetcode
|
|
|
|
func truncateSentence(s string, k int) string {
|
|
end := 0
|
|
for i := range s {
|
|
if k > 0 && s[i] == ' ' {
|
|
k--
|
|
}
|
|
if k == 0 {
|
|
end = i
|
|
break
|
|
}
|
|
}
|
|
if end == 0 {
|
|
return s
|
|
}
|
|
return s[:end]
|
|
}
|