C# 알고리즘 코드카타

50. 가장 가까운 같은 글자

잼잼재미 2023. 12. 6. 09:32

 

 

using System;
using System.Collections.Generic;

public class Solution {
    public int[] solution(string s) {
        List<int> nums = new List<int>();
        Char[] chas = s.ToCharArray();
        int num;
        nums.Add(-1);
        
        for(int i = 1; i < chas.Length; ++i)
        {
            num = -1;
            
            for(int j = 0; j < i; ++j)
            {              
                if(chas[i] == chas[j])
                {
                    num = i - j;
                }               
            }         
            nums.Add(num);
        }
        
        int[] answer = nums.ToArray();             
        return answer;
    }
}

* Dictionary로 풀이 가능

 

'C# 알고리즘 코드카타' 카테고리의 다른 글

52. 콜라 문제  (1) 2023.12.07
51. 푸드 파이트 대회  (0) 2023.12.06
49. 두 개 뽑아서 더하기  (0) 2023.12.05
48. K번째수  (1) 2023.12.04
47. 문자열 내 마음대로 정렬하기  (1) 2023.11.29