Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f3d9813

Browse files
author
Openset
committed
Update: Number of Segments in a String
1 parent 336cae9 commit f3d9813

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package number_of_segments_in_a_string
22

3-
import "regexp"
3+
import "strings"
44

55
func countSegments(s string) int {
6-
reg := regexp.MustCompile(`\S+`)
7-
matches := reg.FindAllString(s, -1)
8-
return len(matches)
6+
return len(strings.Fields(s))
97
}

‎problems/number-of-segments-in-a-string/number_of_segments_in_a_string_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ func TestCountSegments(t *testing.T) {
2525
input: " abc ",
2626
expected: 1,
2727
},
28+
{
29+
input: " abc def ",
30+
expected: 2,
31+
},
2832
{
2933
input: "love live! mu'sic forever",
3034
expected: 4,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /