문제1.
문자열 배열 intStrs와 정수 k, s, l가 주어집니다. intStrs의 원소는 숫자로 이루어져 있습니다.
배열 intStrs의 각 원소마다 s번 인덱스에서 시작하는 길이 l짜리 부분 문자열을 잘라내 정수로 변환합니다. 이때 변환한 정수값이 k보다 큰 값들을 담은 배열을 return 하는 solution 함수를 완성해 주세요.
using System;
using System.Collections.Generic;
public class Solution
{
public int[] solution(string[] intStrs, int k, int s, int l)
{
List<int> answerList= new List<int>();
for(int i =0; i < intStrs.Length; i++)
{
int value = Convert.ToInt32(intStrs[i].Substring(s, l));
if(k < value) answerList.Add(value);
}
return answerList.ToArray();
}
}
문제2
길이가 같은 문자열 배열 my_strings와 이차원 정수 배열 parts가 매개변수로 주어집니다. parts[i]는 [s, e] 형태로, my_string[i]의 인덱스 s부터 인덱스 e까지의 부분 문자열을 의미합니다. 각 my_strings의 원소의 parts에 해당하는 부분 문자열을 순서대로 이어 붙인 문자열을 return 하는 solution 함수를 작성해 주세요.
using System;
public class Solution
{
public string solution(string[] my_strings, int[,] parts)
{
string[] answer = new string[parts.GetLength(0)];
for (int i = 0; i < parts.GetLength(0); i++)
{
int start = parts[i, 0];
int length = parts[i, 1] - start + 1;
answer[i] = my_strings[i].Substring(start, length);
}
return String.Concat(answer);
}
}
문제3.
문자열 my_string과 정수 n이 매개변수로 주어질 때, my_string의 뒤의 n글자로 이루어진 문자열을 return 하는 solution 함수를 작성해 주세요.
using System;
public class Solution
{
public string solution(string my_string, int n)
{
return my_string.Substring(my_string.Length-n, n);
}
}
문제4.
어떤 문자열에 대해서 접미사는 특정 인덱스부터 시작하는 문자열을 의미합니다. 예를 들어, "banana"의 모든 접미사는 "banana", "anana", "nana", "ana", "na", "a"입니다.
문자열 my_string이 매개변수로 주어질 때, my_string의 모든 접미사를 사전순으로 정렬한 문자열 배열을 return 하는 solution 함수를 작성해 주세요.
using System;
public class Solution
{
public string[] solution(string my_string)
{
int strLen = my_string.Length;
string[] answer = new string[strLen];
for (int i = 0; i < strLen; i++)
{
answer[i] = my_string.Substring(i, strLen - i);
}
Array.Sort(answer);
return answer;
}
}
문제5.
어떤 문자열에 대해서 접미사는 특정 인덱스부터 시작하는 문자열을 의미합니다. 예를 들어, "banana"의 모든 접미사는 "banana", "anana", "nana", "ana", "na", "a"입니다.
문자열 my_string과 is_suffix가 주어질 때, is_suffix가 my_string의 접미사라면 1을, 아니면 0을 return 하는 solution 함수를 작성해 주세요.
using System;
public class Solution
{
public int solution(string my_string, string is_suffix)
{
int strLen = my_string.Length;
string[] suffix = new string[strLen];
bool isSuffix =false;
for (int i = 0; i < strLen; i++)
{
suffix[i] = my_string.Substring(i, strLen - i);
if(is_suffix==suffix[i]) isSuffix = true;
}
return isSuffix? 1:0;
}
}
보완점 - 참고. https://devakasha.tistory.com/38
C# String.EndsWith, String.StartsWith메소드
String.EndsWith 메소드public bool EndsWith (string value, bool ignoreCase, System.Globalization.CultureInfo? culture); 이 문자열 인스턴스의 끝 부분과 지정한 문자열이 일치하는지를 확인합니다. String.StartsWith 메소드pu
devakasha.tistory.com
'코딩테스트' 카테고리의 다른 글
| Lv0-11. 리스트(배열) (1) | 2025.01.17 |
|---|---|
| lv0-10. 문자열 (1) | 2025.01.16 |
| Lv0-8. 조건문, 문자열 (1) | 2025.01.15 |
| Lv0-7. 반복문 (0) | 2025.01.13 |
| Lv0-6. 조건문, 반복문 (2) | 2025.01.13 |