Original post

I translated a piece of code from Python to and I did this:

func palindromePairs(words []string) [][]int {
    m := make(map[string]int)
    
    for i, v := range words {
        m[v] = i
    }
    
    ans := [][]int{}
    
    ansSet := make(map[[2]int]struct{})
    
    for idx, word := range words {
        for i := 0; i < len(word) + 1; i++ {
            left, right := word[:i], word[i:]
            
            if isPalindrome(left) {
                if v, ok := m[Reverse(right)]; ok && v != idx {
                    ansSet[[2]int{v, idx}] = struct{}{}
                }
            }
            
            if isPalindrome(right) {
                if v, ok := m[Reverse(left)]; ok && v != idx {
                    ansSet[[2]int{idx, v}] = struct{}{}
                }
            }
        }
    }

    for k := range ansSet {
        ans = append(ans, []int{k[0], k[1]})
    }
    return ans
}

func isPalindrome(s string) bool {
    for i := 0; i < len(s)/2; i++ {
        if s[i] != s[len(s) - i - 1] {
            return false
        }
    }
    
    return true
}

func Reverse(s string) string {
    r := []rune(s)
    for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
        r[i], r[j] = r[j], r[i]
    }
    return string(r)
}

I don’t like the ansSet part where I have to create a [2]int type as maps can’t have a slice as key. Also, while creating the ans, I have to make a new slice because if I just do ans = append(ans, k[:]) it returns the same value in the ans.

Can this be improved without using external packages?