当前位置: > > > > 在 Go 中按字母顺序查找相等分隔的字符串/单词
来源:stackoverflow
2024-04-20 14:48:37
0浏览
收藏
积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《在 Go 中按字母顺序查找相等分隔的字符串/单词》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~
问题内容
我试图找到在字母表的圆形排列中等距分隔的单词/字符串。例如:
- “zzzzyyyybbbzzzaaaaaxxx”是由“xyzab”组成的列表,分隔符为 0 {xy, yz, za, ab}
- “aco”是一个分隔符为 11 {co, oa} 的列表
因此,我想编写函数 isseparated(b) 并在 b 为“isseparated”时返回 true
以下是我的代码/解决方案:
- 首先,我尝试删除字符串中的重复项,以便更容易计算间隔
- 第二,我按字母顺序对字符串进行排序
- 第三,排序后,我计算每个字母的间隔
- 在“isseparated”方法中,我尝试使用
maxpair -1 == count
使其以循环排列方式计数,因为总会有 1 个字母没有配对 [{ab} {bx} {xy} {yz} {za}] - [{0} {21} {0} {0} {0}]]//有5个 对 = maxpair -1({-xy}
因此,由于它是圆形排列的,所以中间的总是奇数,即 21,与其余对的间隔不相等
这是变得棘手的部分,我似乎无法获得所需的输出。按字母顺序查找每个字母的长度/分隔并检查它们是否均匀分隔的正确方法可能是什么。
package main import ( "fmt" "strings" ) //Q3 func separationCount(x, y string) int { alphabets := [26]string{"a","b","c","d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u","v", "w", "x", "y", "z"} separation := 0 for i:=0; i < len(alphabets); i++{ if x == alphabets[i]{ for j:= i+1; j <len(alphabets); j++{ if y == alphabets[i+1]{ fmt.Println(separation) return separation }else{ i++ separation++ } } }else{ //do nothing } } //fmt.Println(separation) return 0 } func isSeparated(B [] string) bool { var N int = len(B) - 1 var maxPair int var item1 string var item2 string var separation int = 0 count := 0 var intialSeparation int //calling the methods fmt.Println("Original array:",B) B = removeDuplicates(B) B = sortedList(B) item1 = B[0] item2 = B[1] intialSeparation = separationCount(item1,item2) for i := 0; i< N; i++{ item1 = B[i] item2 = B[i + 1] separation = separationCount(item1,item2) maxPair++ if intialSeparation == separation{ count++ } if maxPair == count{ return true }else{ return false } } return false } //to sort the alphabets func sortedList(B []string) [] string { N := len(B) //max := 0 element1 := 0 element2 := 1 for element2 < N { var item1 string = B[element1] var item2 string = B[element2] //using function call if greater(item1, item2){ B[element1] = item2 B[element2] = item1 } element1++ element2++ } fmt.Println("Alphabetically sorted:", B ) return B } //for sorting func greater(a, b string) bool { if strings.ToLower(a) > strings.ToLower(b) { return true } else { return false } } //removing duplicates func removeDuplicates(B []string) []string { encountered := map[string]bool{} // Create a map of all unique elements. for v:= range B { encountered[B[v]] = true } // Place all keys from the map into a slice. result := []string{} for key, _ := range encountered { result = append(result, key) } fmt.Println("Duplicates removed:", result ) return result } func main(){ //q3 B := []string{"y", "a", "a", "a", "c", "e", "g", "w", "w", "w"} fmt.Println(isSeparated(B)) }
解决方案
我不太明白你试图确定分离的部分。在 go 中,就像在 c 中一样,您可以对字符进行算术运算。例如,您将获得每个小写字母的从 0 开始的索引:
pos := char - 'a';
您可以将 "abxyz"
转为
{0, 1, 23, 24, 25}.
如果你计算相邻字母之间的差异,你会得到
{-25, 1, 22, 1, 1}
(-25 是最后一个值和第一个值之间的差值。)有两个间隙:一个是循环在 b 和 w 之间开始,另一个是字母换行。第二个间隙是差值为负的地方,总是在最后一项和第一项之间。您可以在差值上加上 26 来调整它,也可以使用模算术,其中使用余数 %
来计算环绕:
diff := ((p - q + 26) % 26;
如果第一个操作数为正,则 %
会强制结果范围为 0 到 25。 + 26 强制其为正数。 (下面的程序使用 25,因为您对分隔的定义不是位置的差异,而是之间的过滤器数量。)
现在你有了差异
{1, 1, 22, 1, 1}
当您最多只有两个不同的值并且其中一个最多出现一次时,您的条件就得到满足。 (我发现这个条件测试起来非常复杂,见下文,但部分原因是 go 的映射有点麻烦。)
无论如何,这是代码:
package main import "fmt" func list(str string) int { present := [26]bool{} pos := []int{} count := map[int]int{} // determine which letters exist for _, c := range str { if 'a' <= c && c <= 'z' { present[c-'a'] = true } } // concatenate all used letters (count sort, kinda) for i := 0; i < 26; i++ { if present[i] { pos = append(pos, i) } } // find differences q := pos[len(pos)-1] for _, p := range pos { diff := (p - q + 25) % 26 count[diff]++ q = p } // check whether input is a "rambai" if len(count) > 2 { return -1 } which := []int{} occur := []int{} for k, v := range count { which = append(which, k) occur = append(occur, v) } if len(which) < 2 { return which[0] } if occur[0] != 1 && occur[1] != 1 { return -1 } if occur[0] == 1 { return which[1] } return which[0] } func testme(str string) { fmt.Printf("\"%s\": %d\n", str, list(str)) } func main() { testme("zzzzyyyybbbzzzaaaaaxxx") testme("yacegw") testme("keebeebheeh") testme("aco") testme("naan") testme("mississippi") testme("rosemary") }
今天关于《在 Go 中按字母顺序查找相等分隔的字符串/单词》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注公众号!