Custom Sort String


  • 0

    Click here to see the full article post


  • 0
    B

    for (int i = 0; i < count[c - 'a']; ++i)
      ans.append(c);
    count[c - 'a'] = 0;

    can be simplified as
    while (count[c-'a']-- > 0)
      ans.append(c);


  • 1
    4

    From https://docs.python.org/2/library/collections.html#collections.Counter

    A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.


  • 0
    K

    Could you clarify why this loop is only O(S.length)?

            for (char c: S.toCharArray()) {
                for (int i = 0; i < count[c - 'a']; ++i)
                    ans.append(c);
                // Setting count[char] to zero to denote that we do
                // not need to write 'char' into our answer anymore.
                count[c - 'a'] = 0;
            }
    

    Worse case, can't the inner loop be O(T.length)? (perhaps the case where you have S being 'a' and T being 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')

    Thanks!


  • 0
    A

    @bl2003 I guess, code readability is more important than the number of lines


  • 0
    L

    Collection is not defined man. It is easy.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.