1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| func finalString(s string) string { queues := [][]rune{ []rune{}, []rune{}, } for _, ch := range s { if ch == 'i' { queues[0], queues[1] = queues[1], queues[0] } else { queues[0] = append(queues[0], ch) } } sb := strings.Builder{} n1 := len(queues[0]) n2 := len(queues[1]) for i := n2 - 1; i >= 0; i-- { sb.WriteString(string(queues[1][i])) } for i := 0; i < n1; i++ { sb.WriteString(string(queues[0][i])) } return sb.String() }
|