Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs/algorithms/algs/016/ #39

Open
utterances-bot opened this issue Jan 30, 2022 · 1 comment
Open

docs/algorithms/algs/016/ #39

utterances-bot opened this issue Jan 30, 2022 · 1 comment

Comments

@utterances-bot
Copy link

第 016 期(2022.1.27) | Learning

题目描述 # 句子仅由小写字母(‘a’ 到 ‘z’)、数字(‘0’ 到 ‘9’)、连字符('-')、标点符号('!'、'.' 和 ‘,')以及空格(’ ‘)组成。每个句子可以根据空格分解成 一个或者多个 token ,这些 token 之间由一个或者多个空格 ' ' 分隔。 如果一个 token 同时满足下述条件,则认为

https://talkgo.dev/docs/algorithms/algs/016/

Copy link
Contributor

func countValidWords(sentence string) int {
words := strings.Fields(sentence)
cunt := 0
CuntWord:
for _, word := range words {
sum := 0
n := len(word)
for i := 0; i < n; i++ {
if unicode.IsLetter(rune(word[i])) {
continue
} else if unicode.IsDigit(rune(word[i])) {
continue CuntWord
} else if word[i] == '-' {
if sum == 1 {
continue CuntWord
}
sum++
if i == 0 || i == n-1 || !unicode.IsLetter(rune(word[i-1])) || !unicode.IsLetter(rune(word[i+1])) {
continue CuntWord
break
}
} else if i != n-1 {
continue CuntWord
break
}
}
cunt++
}
return cunt
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants