1742. Orderly Queue

ode(Language:C++)
class Solution {
public:
    /**
     * @param S: a string
     * @param K: int
     * @return: the lexicographically smallest string
     */
    string orderlyQueue(string &S, int K) {
        // Write your code here.
          if (K > 1) {
            sort(S.begin(), S.end());
            return S;
        }
        string res = S;
        for (int i = 1; i < S.length(); i++)
            res = min(res, S.substr(i) + S.substr(0, i));
        return res;

    }
};

Comments

Popular posts from this blog

算法的比较