๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ž์—ด ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ์ •๋ ฌํ•˜๊ธฐ Swift

by Fomagran ๐Ÿ’ป 2020. 2. 13.
728x90
๋ฐ˜์‘ํ˜•

๋‚˜์˜ ํ’€์ด:

1.strings ์ค‘ n๋ฒˆ์งธ๋ฅผ ์ถ”๋ ค๋‚ธ๋‹ค.

2.n๋ฒˆ์จฐ์˜ ๊ฐ’๋“ค์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.

3.n๋ฒˆ์จฐ์˜ ๊ฐ’๋“ค ์ค‘ ์ค‘๋ณต์ด ์žˆ๋Š” ์ง€ ํ™•์ธํ•œ๋‹ค.

4.์ค‘๋ณต์ด ์žˆ๋Š” ๊ฐ’๋“ค์„ ๋‹ด๋Š”๋‹ค.

5.n๋ฒˆ์งธ์˜ ๊ฐ’์ด ์ค‘๋ณต์ด๋ผ๋ฉด strings ์ค‘ n๋ฒˆ์จฐ๊ฐ€ ์ค‘๋ณต์ธ ๊ฒƒ๋“ค์„ ์ฐพ์•„์„œ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋’ค result์— ๋„ฃ์–ด์ค€๋‹ค.

6.์ค‘๋ณต์ด ์•„๋‹ˆ๋ผ๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœํ•œ ๋ฐฐ์—ด์„ ๋ฐ”ํƒ•์œผ๋กœ strings ์ค‘ n๋ฒˆ์งธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด์„ ์ฐพ์•„์„œ result์— ๋„ฃ์–ด์ค€๋‹ค.

 

ํ’€๋ฉด์„œ๋„ ์ด๋ ‡๊ฒŒ ํ‘ธ๋Š” ๊ฑด ์•„๋‹๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. ์˜ค์ง€๊ฒŒ ๋ณต์žกํ•˜๊ฒŒ ํ’€์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import Foundation
 
func solution(_ strings:[String], _ n:Int-> [String] {
    var result = [String]()
     var narr = [Character]()
    var overlap = [Character:Int]()
    let count = 1
    var overlapset:Set<Character> = []
    var sortoverlap = [String]()
 
1.strings ์ค‘ n๋ฒˆ์งธ๋ฅผ ์ถ”๋ ค๋‚ธ๋‹ค.
    for i in 0..<strings.count{
        narr.append(strings[i].map{$0}[n])
    }
2.n๋ฒˆ์จฐ์˜ ๊ฐ’๋“ค์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
    let sortnarr = narr.sorted()
   
3.n๋ฒˆ์จฐ์˜ ๊ฐ’๋“ค ์ค‘ ์ค‘๋ณต์ด ์žˆ๋Š” ์ง€ ํ™•์ธํ•œ๋‹ค.
 for i in 0..<sortnarr.count{
        if overlap[sortnarr[i]] != nil {
        overlap[sortnarr[i]] = count + 1
        } else{
        overlap[sortnarr[i]] = count
        }
    }
 
    let sortdic = overlap.keys.sorted()
   4.์ค‘๋ณต์ด ์žˆ๋Š” ๊ฐ’๋“ค์„ ๋‹ด๋Š”๋‹ค.
 for i in 0..<sortnarr.count {
        if overlap[sortnarr[i]]! > 1 {
    }
 
    for i in 0..<sortdic.count{
        sortoverlap.removeAll()
5.๋งŒ์•ฝ ์ค‘๋ณต์ด๋ผ๋ฉด strings ์ค‘ n๋ฒˆ์จฐ๊ฐ€ ์ค‘๋ณต์ธ ๊ฒƒ๋“ค์„ ์ฐพ์•„์„œ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋’ค result์— ๋„ฃ์–ด์ค€๋‹ค.
 
                if overlapset.contains(sortdic[i]) {
                    for j in 0..<strings.count{
                        let c = strings[j].map{$0}
                        if sortdic[i] == c[n] {
                            sortoverlap.append(strings[j])
                        }
 
                    }
                    sortoverlap.sort(by:<)
                    for j in 0..<sortoverlap.count{
                        result.append(sortoverlap[j])
                    }
6.๋งŒ์•ฝ ์ค‘๋ณต์ด ์•„๋‹ˆ๋ผ๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœํ•œ ๋ฐฐ์—ด์„ ๋ฐ”ํƒ•์œผ๋กœ strings ์ค‘ n๋ฒˆ์งธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด์„ ์ฐพ์•„์„œ result์— ๋„ฃ์–ด์ค€๋‹ค.
                }else{
                    for j in 0..<strings.count{
                        let c = strings[j].map{$0}
                        if sortdic[i] == c[n] {
                            result.append(strings[j])
                        }
                    }
        }
 
        }
 
    return result
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

๋‹ค๋ฅธ ์‚ฌ๋žŒ ์ค‘ ๊ฐ€์žฅ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š” ํ’€์ด

๋‹น์—ฐํ•˜๊ฒŒ๋„ ์•„์ฃผ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋‹ค. sorted์•ˆ์— element๋“ค์„ ์ด์šฉํ•ด ํ‘ธ๋Š”๊ฑด๋ฐ sorted๋Š” ์•Œ์•„์„œ element 2๊ฐœ๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ  ์กฐ๊ฑด์„ ๋„ฃ์–ด์ฃผ๋ฉด ์•Œ์•„์„œ ์ •๋ ฌํ•ด์ค€๋‹ค.

1.๋ฌธ์ž์—ด์˜ n๋ฒˆ์งธ๋ฅผ ์•Œ๊ธฐ ์œ„ํ•ด์„  index์˜ ์œ„์น˜๋ฅผ ์•Œ์•„์•ผํ•˜๋Š”๋ฐ ์–ด์ฐจํ”ผ ๋ชจ๋“  ๋ฌธ์ž์—ด์˜ n๋ฒˆ์งธ๋Š” ๋˜‘๊ฐ™์œผ๋ฏ€๋กœ ์ž„์˜๋กœ 0๋ฒˆ์งธ ๋ฌธ์ž์—ด์˜ 0๋ฒˆ์งธ์—์„œ n๋งŒํผ ๋–จ์–ด์ง„ ๊ณณ์˜ index ์ฆ‰ n๋ฒˆ์งธ์˜ index๋ฅผ ์•Œ์•„๋‚ธ ๋’ค

2.result๋ผ๋Š” ์ƒ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  strings์— sorted๋ฉ”์†Œ๋“œ๋ฅผ ์ ์šฉํ•œ ๋’ค element์— ๊ฐ ๊ฐ s1,s2๋ฅผ ๋„ฃ์–ด์ค€ ๋’ค ์กฐ๊ฑด์€ s1[index]์™€ s2[index]๊ฐ€ ๊ฐ™์„ ๋•Œ ์ฆ‰, n๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ๊ฐ™์„ ๋• s1 ๊ณผ s2๋ฅผ ๋น„๊ตํ•ด์„œ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด๋ผ

n๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ๊ฐ™์ง€ ์•Š์„ ๋•Œ๋Š” s1[index]์™€ s2[index]๋ฅผ ๋น„๊ตํ•ด์„œ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋„ฃ์–ด๋ผ.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import Foundation
 
func solution(_ strings:[String], _ n:Int-> [String] {
    let index = strings[0].index(strings[0].startIndex, offsetBy: n)
 
    let result = strings.sorted(by : {(s1 : String, s2 : String-> Bool in
        if s1[index] == s2[index] {
            return s1 < s2
        }
        else {
            return s1[index] < s2[index]
        }
    })
 
    return result
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€