Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- SW Expert Academy
- ubuntu
- 코로나19
- 파이썬
- github
- 프로그래머스 파이썬
- leetcode
- Git
- 데이콘
- 금융문자분석경진대회
- Docker
- Baekjoon
- 백준
- ChatGPT
- 맥북
- 캐치카페
- dacon
- 프로그래머스
- PYTHON
- Kaggle
- hackerrank
- AI 경진대회
- programmers
- Real or Not? NLP with Disaster Tweets
- 더현대서울 맛집
- 편스토랑 우승상품
- 우분투
- gs25
- 자연어처리
- 편스토랑
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 1451. Rearrange Words in a Sentence (Python) 본문
Programming/코딩 1일 1문제
[leetCode] 1451. Rearrange Words in a Sentence (Python)
솜씨좋은장씨 2021. 1. 20. 00:15728x90
반응형
Given a sentence text (A sentence is a string of space-separated words) in the following format:
- First letter is in upper case.
- Each word in text are separated by a single space.
Your task is to rearrange the words in text such that all words are rearranged in an increasing order of their lengths. If two words have the same length, arrange them in their original order.
Return the new text following the format shown above.
Example 1:
Input: text = "Leetcode is cool"
Output: "Is cool leetcode"
Explanation: There are 3 words, "Leetcode" of length 8, "is" of length 2 and "cool" of length 4.
Output is ordered by length and the new first word starts with capital letter.
Example 2:
Input: text = "Keep calm and code on"
Output: "On and keep calm code"
Explanation: Output is ordered as follows:
"On" 2 letters.
"and" 3 letters.
"keep" 4 letters in case of tie order by position in original text.
"calm" 4 letters.
"code" 4 letters.
Example 3:
Input: text = "To be or not to be"
Output: "To be or to be not"
Constraints:
- text begins with a capital letter and then contains lowercase letters and single space between words.
- 1 <= text.length <= 10^5
Solution
class Solution:
def arrangeWords(self, text: str) -> str:
word_list = text.split(" ")
info_list = []
for i, word in enumerate(word_list):
info_list.append([i, len(word), word])
sorted_list = sorted(info_list, key=lambda x: (x[1], x[0]))
word_list = [word[2].lower() for word in sorted_list]
character_list = list(word_list[0])
character_list[0] = character_list[0].upper()
word_list[0] = "".join(character_list)
return " ".join(word_list)
Solution 풀이
먼저 split을 통하여 text를 단어단위로 나누어줍니다.
그 다음 [ index, 단어의 길이, 단어 ] 와 같은 형태의 리스트를 만들어줍니다.
만든 다음 단어의 길이, index 순서 기준으로 sorting 합니다.
sorting 한 리스트에서 단어만 뽑아오는데 이때 각 단어에서의 대문자를 모두 소문자로 바꾸어 줍니다.
그 다음 맨 처음에 존재하는 단어의 맨 앞글자를 대문자로 바꾸어줍니다.
마지막으로 다시 하나의 text로 join을 해주면 됩니다.
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 648. Replace Words (Python) (0) | 2021.01.22 |
---|---|
[leetCode] 80. Remove Duplicates from Sorted Array II (Python) (0) | 2021.01.21 |
[leetCode] 1592. Rearrange Spaces Between Words (Python) (0) | 2021.01.19 |
[leetCode] 876. Middle of the Linked List (Python) (0) | 2021.01.18 |
[leetCode] 717. 1-bit and 2-bit Characters (Python) (0) | 2021.01.17 |
Comments