':type S: str
:type K: int
:rtype: str'

def licenseKeyFormatting(self, S, K):

    result = []
    for i in reversed(xrange(len(S))):
        if (S[i] == '-'):
            continue
        if ((len(result) % (K + 1)) == K):
            result += '-'
        result += S[i].upper()
    return ''.join(reversed(result))
