๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
3๏ธโƒฃ Swift/Problem Solving

[Swift Algorithm] ๋ฌธ์ž์—ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ Programmers(Lv.1)

by seolhee2750 2021. 6. 17.
๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ž์—ด s์— ๋‚˜ํƒ€๋‚˜๋Š” ๋ฌธ์ž๋ฅผ ํฐ๊ฒƒ๋ถ€ํ„ฐ ์ž‘์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.
s๋Š” ์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋ณด๋‹ค ์ž‘์€ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค.

 

์ œํ•œ ์กฐ๊ฑด
  • str์€ ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ
s return
"Zbcdefg" "gfedcbZ"

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
func solution(_ s:String) -> String {
    return String(s.sorted(by: >))
}
  • sorted ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฆฌํ„ดํ•ด์ฃผ์—ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ , ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋ณด๋‹ค ์ž‘์€ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค,, ์ด๋Ÿฐ ๋ง์ด ์žˆ์–ด์„œ ๋”ฐ๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผ ํ•˜๋Š” ๊ฑด๊ฐ€ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ์›๋ž˜ ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋ณด๋‹ค ๋จผ์ €?๋ผ์„œ ์˜ˆ๋ฅผ ๋“ค๋ฉด, "123"์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๋ฉด "321"์ด ๋˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ, "Aab"๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๋ฉด "baA"์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค!
  • ์œ„ ์ฒ˜๋Ÿผ ๋ฌธ์ž์—ด์˜ ์ •๋ ฌ์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ์ •ํ™•ํžˆ ์•Œ๊ณ  ๋‚˜๋‹ˆ ๋„ˆ๋ฌด ์‰ฌ์šด ๋ฌธ์ œ์˜€๋‹ค.

 


 

๋ฌธ์ œ

https://programmers.co.kr/learn/courses/30/lessons/12917

๋Œ“๊ธ€