在Go中按字母顺序查找均等的字符串/单词

问题描述 投票:0回答:1

我正在尝试查找在字母的圆形排列中均等分开的单词/字符串。例如:

  • “ 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}]]//there are 5 pairs = 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))
}



我正在尝试查找在字母的圆形排列中均等分开的单词/字符串。例如:“ zzzzyyyybbbzzzaaaaaxxx”是由“ xyzab”组成的列表,其分隔为0 {xy,...

algorithm go alphabetical
1个回答
1
投票

我不太了解您尝试确定分隔的部分。在Go中,就像在C中一样,您可以对字符进行算术运算。例如,您将获得每个小写字母的从0开始的索引:

pos := char - 'a';

您可以将"abxyz"转到

© www.soinside.com 2019 - 2024. All rights reserved.