Leetcode Note: Go - Rearrange Characters to Make Target String

Rearrange Characters to Make Target String - LeetCode
https://leetcode.com/problems/rearrange-characters-to-make-target-string/

  • Go 言語で取り組んだメモ

回答

0ms, 100% Easy you understand - Rearrange Characters to Make Target String - LeetCode
https://leetcode.com/problems/rearrange-characters-to-make-target-string/solutions/3170583/0ms-100-easy-you-understand/

func rearrangeCharacters(s string, target string) int {
    str := make([]int, 26)

    for _, v := range s {
        str[v - 'a']++
    }

    res := 0
    for {   
        count := 0
        for _, v := range target {
            if str[v - 'a'] != 0 {
                count++
                str[v - 'a']--
            }   
        }
        if count != len(target) {
            break
        }else{
            res++
        }
    }

    return res
}