Last active
May 17, 2020 11:38
-
-
Save zhangguanzhang/791c484f6137f8deaed473a3857219ee to your computer and use it in GitHub Desktop.
golang Remove the specified repeated string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
s := `aaabccaad` | |
u := "a" | |
fmt.Printf("%s %s ===> %s \n", s, u, uniq(s, u)) | |
s = `hvafdaavhhaav` | |
fmt.Printf("%s %s ===> %s \n", s, u, uniq(s, u)) | |
u = "h" | |
fmt.Printf("%s %s ===> %s \n", s, u, uniq(s, u)) | |
} | |
func uniq(s string, uniq string) string { | |
var ( | |
old uint8 | |
count int | |
result string | |
) | |
for i := 0; i < len(s); i++ { | |
if string(s[i]) == uniq { | |
count++ | |
if count == 1 && old != 0 { | |
result += string(old) | |
} | |
} else { | |
if count == 0 && old != 0 { | |
result += string(old) | |
} | |
if count > 0 { | |
result += string(old) | |
count = 0 | |
} | |
} | |
old = s[i] | |
} | |
result += string(old) | |
return result | |
} |
Author
zhangguanzhang
commented
May 17, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment