반응형
SMALL
문제

풀이
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Two_Sum
{
class Program
{
static void Main()
{
string str = Console.ReadLine(); //문자 입력
string str2 = new string(str.Reverse().ToArray()); //입력받은 문자 뒤집기
if (str == str2) { Console.WriteLine(str.Length); } //입력받은 문자가 팰린드롬인지 아닌지 보기
else
{
for(int i = 0; i <= str.Length; i++) //아니라면 단어 길이 만큼 돌며 탐색
{
str2 = new string(str.Substring(0, i).Reverse().ToArray()); //str2는 기존 단어에서 첫번째 글자부터 뒤집어질 문자들
string strs = str + str2; // str2를 입력문자 뒤에 붙여 새로운 문자열 변수 초기화
string str3 = new string(strs.Reverse().ToArray()); // 팰린드롬 확인용 문자열 변수 초기화
if (strs == str3) { Console.WriteLine(strs.Length); break; } //팰린드롬이 맞다면 break; 후 글자 길이 출력
}
}
}
}
}
팰린드롬인지 입력글자의 길이 -1 부터 하나하나씩 붙여보니 이게 최고의 비효율인지 알게되었다;;
이후 찾아보니 입력문자열의 0번 인덱스 부터 뒤집어서 붙이면 더 효율적으로 팰린드롬을 찾을수 있다는 걸 확인 앞으로 유용하게 사용할듯하다.
반응형
LIST
'Algorithm > BOJ' 카테고리의 다른 글
| [프로그래머스] 다트게임 C#풀이 (0) | 2023.01.23 |
|---|---|
| [BOJ] 10822 더하기 C# 사용 풀이 (0) | 2023.01.23 |
| [BOJ] 1159 농구경기 c# 사용 문제풀이 (2) | 2023.01.22 |
| [BOJ] 1251 단어 나누기 C# 풀이 (1) | 2023.01.22 |
| [BOJ] 1439 뒤집기 C# 풀이 (1) | 2023.01.20 |