Skip to content

Commit

Permalink
update string leetcode 2047
Browse files Browse the repository at this point in the history
  • Loading branch information
dowell2020 committed Jan 30, 2022
1 parent 50cb959 commit 4e219fa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
37 changes: 37 additions & 0 deletions strings/leet2047.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package strings

import (
"strings"
"unicode"
)

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
}
13 changes: 13 additions & 0 deletions strings/leet2047_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package strings

import (
"fmt"
"testing"
)

func TestCountValidWords(t *testing.T) {
str := "cat and dog"
// str := "!this 1-s b8d!"
res := countValidWords(str)
fmt.Println(res)
}
5 changes: 5 additions & 0 deletions strings/strings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### 字符串相关操作方法

* strings.Fields()

* strings.ContainsRune()
7 changes: 7 additions & 0 deletions strings/unicode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### 字节相关操作方法

* unicode.IsLetter

* unicode.IsDigit

* unicode.IsLowe

0 comments on commit 4e219fa

Please sign in to comment.