• + 0 comments

    My solution in Go

    var topicsResult = make([]int32, 0)
    
    func acmTeam(topics []string) []int32 {
        for i := 0; i < len(topics); i++ {
            slicedTopics := copyTopics(i+1, topics)
            handlePair(slicedTopics, topics[i], 0)
        }
    
        maxValue := findMax()
        foo := countBy(maxValue)
        return []int32{maxValue, foo}
    }
    
    func countBy(value int32) int32 {
        counter := 0
    
        for i := 0; i < len(topicsResult); i++ {
            if topicsResult[i] == value {
                counter++
            }
        }
    
        return int32(counter)
    }
    
    func findMax() int32 {
        if len(topicsResult) == 0 {
            return 0
        }
    
        maxValue := topicsResult[0]
    
        for i := 1; i < len(topicsResult); i++ {
            if maxValue < topicsResult[i] {
                maxValue = topicsResult[i]
            }
        }
    
        return maxValue
    }
    
    func handlePair(topics []string, topic string, j int32) {
        if j >= int32(len(topics)) {
            return
        }
    
        knowTopics := make([]string, len(topic))
    
        for i := 0; i < len(topic); i++ {
            if string(topic[i]) == "1" {
                knowTopics[i] = "1"
            } else if string(topics[j][i]) == "1" {
                knowTopics[i] = "1"
            } else {
                knowTopics[i] = "0"
            }
        }
    
        j++
        counter := 0
    
        for i := 0; i < len(knowTopics); i++ {
            if knowTopics[i] == "1" {
                counter++
            }
        }
    
        topicsResult = append(topicsResult, int32(counter))
        handlePair(topics, topic, j)
    }
    
    func copyTopics(startIndex int, topics []string) []string {
        copyStrings := make([]string, len(topics)-startIndex)
    
        for i := 0; i < len(copyStrings); i++ {
            copyStrings[i] = topics[startIndex]
            startIndex++
        }
    
        return copyStrings
    }