Programmer:)
[ 프로그래머스 / C# ] 옹알이 (1) 본문
반응형
문제 :
https://school.programmers.co.kr/learn/courses/30/lessons/120956
내가 제출한 코드 :
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int solution(string[] babbling) {
int answer = 0;
string blank = "0";
List<string> mainList = new List<string> { "aya", "ye", "woo", "ma" };
List<string> inputList = babbling.ToList();
List<string> search = new List<string>();
//인풋값중에 발음할 수 있는 단어와 완전히 일치하는 것 부터 카운터 한 뒤 제거
search = inputList.Intersect(mainList).ToList();
if(search.Count != 0)
{
answer = search.Count;
inputList = inputList.Except(search).ToList();
search = new List<string>();
}
//완전히 일치하는 단어가 없는 인풋값. 하나씩 가져가다 대조작업
for(int i = 0; i < inputList.Count; i++)
{
string inputString = inputList[i];
for (int j = 0; j < mainList.Count; j++)
{
string main = mainList[j];
if (inputString.Contains(main))
{
inputString = inputString.Replace(main, blank);
if (inputString.All(char.IsDigit))
{
answer++;
break;
}
}
}
}
return answer;
}
}
Lv 0 문제였는데 난 조금 헤맨 문제
Contains로 비교 작업 후 삭제를 하게끔 진행을 했었는데
"wyeoo" 일경우 "ye" 일치 후 삭제 진행 "woo"로 남아 카운트 되는 현상 발생.
삭제 대신 숫자를 대신 집어넣고 전부 숫자로 변환 가능한지 체크 후 카운트 하게끔 수정했다.
숫자로 변환 한 이유는 isDigit을 사용하기 위해 숫자로 넣어줌.
(다른 좋은 방식이 생각나지 않았다.)
다른사람이 푼 코드 (다른 좋은 방식 ...)
public int solution(string[] babbling) {
return babbling.Select(s=> s.Replace("ayaaya", "?").Replace("yeye", "?").Replace("woowoo", "?").Replace("mama", "?").Replace("aya", "!").Replace("ye", "!").Replace("woo", "!").Replace("ma", "!").All(ch=>ch=='!') ? 1 : 0).Sum();
}
쿼리식으로 한줄로 끝내버리셨다....
쿼리식을 연습하고 있는데 아직 이런 조건들을 반영하여 결과값 도출하는 부분이 어렵다 ㅠ
반응형
'프로그래머스 정복 > C#' 카테고리의 다른 글
최댓값과 최솟값 (Select 이용) (0) | 2023.04.03 |
---|---|
[ 프로그래머스 / C# ] 혼자놀기의 달인 (0) | 2022.11.18 |
[ 프로그래머스 / C# ] 숫자 카드 나누기 (0) | 2022.11.18 |
[ 프로그래머스 / C# ] 과일 장수 (0) | 2022.11.16 |
[ 프로그래머스 / C# ] K번째 수 (0) | 2022.03.29 |
Comments