엔지니어 게시판
LeetCode 솔루션 분류

[11/6] 899. Orderly Queue

컨텐츠 정보

본문

Hard
1338536Add to ListShare

You are given a string s and an integer k. You can choose one of the first k letters of s and append it at the end of the string..

Return the lexicographically smallest string you could have after applying the mentioned step any number of moves.

 

Example 1:

Input: s = "cba", k = 1
Output: "acb"
Explanation: 
In the first move, we move the 1st character 'c' to the end, obtaining the string "bac".
In the second move, we move the 1st character 'b' to the end, obtaining the final result "acb".

Example 2:

Input: s = "baaca", k = 3
Output: "aaabc"
Explanation: 
In the first move, we move the 1st character 'b' to the end, obtaining the string "aacab".
In the second move, we move the 3rd character 'c' to the end, obtaining the final result "aaabc".

 

Constraints:

  • 1 <= k <= s.length <= 1000
  • s consist of lowercase English letters.
Accepted
52,225
Submissions
79,582
태그

관련자료

댓글 2

학부유학생님의 댓글

  • 익명
  • 작성일
Runtime: 64 ms, faster than 44.05% of Python3 online submissions for Orderly Queue.
Memory Usage: 13.8 MB, less than 72.62% of Python3 online submissions for Orderly Queue.
class Solution:
    def orderlyQueue(self, s: str, k: int) -> str:
        lexi_min = s
        if k>1:
            lexi_min = "".join(sorted(list(s)))
        else:
            for i in range(len(s)):
                lexi_min = min(lexi_min, s[i:]+s[:i])
        
        return lexi_min

jihoonjohnkim님의 댓글

  • 익명
  • 작성일
JAVA by jihoonjohnkim
class Solution {
    public String orderlyQueue(String s, int k) {
        if(k==1){
            String answer = s;
            for (int i = 0; i < s.length(); ++i) {
                String strMax = s.substring(i) + s.substring(0, i);
                if (strMax.compareTo(answer) < 0) {
                    answer = strMax;
                }
            }
            return answer;
        }else{           
            char[] strArr = s.toCharArray();
            Arrays.sort(strArr);
            return new String(strArr);
        }
    }
}
전체 410 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 637 명
  • 오늘 방문자 7,610 명
  • 어제 방문자 9,517 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,599 명
알림 0