-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshoppingsummarycounter.go
44 lines (38 loc) · 1.01 KB
/
shoppingsummarycounter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package piscine
func ShoppingSummaryCounter(str string) map[string]int {
// Initialize a map to store the count of each item
itemCount := make(map[string]int)
// Variable to hold the current item
var currentItem string
spaceCount := 0
// Iterate over each character in the string
for i := 0; i < len(str); i++ {
char := str[i]
// If the character is a space, it means we've reached the end of an item
if char == ' ' {
spaceCount++
// If currentItem is not empty, add it to the map
if currentItem != "" {
itemCount[currentItem]++
currentItem = ""
}
} else {
// Add spaces to the map before starting a new item
if spaceCount > 0 {
itemCount[" "] += spaceCount
spaceCount = 0
}
// Otherwise, keep building the current item
currentItem += string(char)
}
}
// Add the last item to the map if there's any
if currentItem != "" {
itemCount[currentItem]++
}
// Add any trailing spaces to the map
if spaceCount > 0 {
itemCount[" "] += spaceCount
}
return itemCount
}