• + 0 comments

    golang:

    func biggerIsGreater(w string) string {
        str := strings.Split(w, "")
    
        s_len := len(str)
    
        for i := 1; i < len(str); i++ {
    
            curr_index := s_len - i - 1
            curr := str[curr_index]
    
            rest := str[curr_index+1:]
    
            sort.Slice(rest, func(i, j int) bool {
                return rest[i] < rest[j]
            })
    
            for j, v := range rest {
                if v > curr {
                    initial := strings.Join(str[:curr_index], "")
                    rest = append(rest[:j], rest[j:]...)
    
                    rest[j] = curr
    
                    sort.Slice(rest, func(i, j int) bool {
                        return rest[i] < rest[j]
                    })
    
                    return initial + v + strings.Join(rest, "")
                }
            }
    
        }
    
        return "no answer" 
    }