Intern
/** :-))) :-(( "ab :-)" = "ab " "ab :-)))" = "ab " "ab :-)))(" = "ab (" "ab ):-)" = "ab )" ":-)" = "" **/ func deleteP(str string) string { n := len(str) res := make([]byte, 0, n) i := 0 for i < n { if str[i] == ':' && str[i+1] == '-' && (str[i+2] == ')' || str[i+2] == '(') { ch := str[i+2] i += 2 for i < n && str[i] == ch { i++ } } else { res = append(res, str[i]) } } return string(res) }
sobes.tech AI
Answer from AI
The deleteP function in Go removes all occurrences of emoticons in the format ":-)))" or ":-((" with any number of repeated parentheses ")" or "(" after ":-".
Algorithm:
- Iterate through the string character by character.
- If a sequence ":-" is encountered followed by the same parentheses ")" or "(", skip all of them.
- Otherwise, add the character to the result.
Example:
func deleteP(str string) string {
n := len(str)
res := make([]byte, 0, n)
i := 0
for i < n {
if i+2 < n && str[i] == ':' && str[i+1] == '-' && (str[i+2] == ')' || str[i+2] == '(') {
ch := str[i+2]
i += 3
for i < n && str[i] == ch {
i++
}
} else {
res = append(res, str[i])
i++
}
}
return string(res)
}
Examples:
"ab :-)"->"ab ""ab :-)))"->"ab ""ab :-)))("->"ab (""ab ):-)"->"ab )"":-)"->""