5923. 从房屋收集雨水需要的最少水桶数
https://leetcode-cn.com/problems/minimum-number-of-buckets-required-to-collect-rainwater-from-houses/
- 提交时间:2021-11-28 05:18:17
- 执行用时:12 ms, 在所有 Go 提交中击败了100.00%的用户
- 内存消耗:6.2 MB, 在所有 Go 提交中击败了100.00%的用户
- 通过测试用例:207 / 207
func minimumBuckets(street string) (ans int) {
n := len(street)
if street == "." {
return 0
}
if street == "H" {
return -1
}
if strings.Contains(street, "HHH") {
return -1
}
if street[0] == 'H' && street[1] == 'H' {
return -1
}
if street[n-1] == 'H' && street[n-2] == 'H' {
return -1
}
street = strings.Replace(street, "H.H", "hBh", -1)
street = strings.Replace(street, ".H", "Bh", -1)
street = strings.Replace(street, "H.", "hB", -1)
return strings.Count(street, "B")
}