Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[백준] 17609번 회문 #41

Open
CMSSKKK opened this issue Oct 24, 2022 · 1 comment
Open

[백준] 17609번 회문 #41

CMSSKKK opened this issue Oct 24, 2022 · 1 comment
Assignees

Comments

@CMSSKKK
Copy link
Member

CMSSKKK commented Oct 24, 2022

TITLE

회문
(KOI 2019)

LINK

📷 Screenshots

댓글 양식

  • 아래 양식을 복사한 뒤 [shift]+[tab] 2회를 하고 작성하여 주세요
    ### 풀이 언어

    - python/java

    ### 코드

    ```python/java

    ```

    ### 핵심 로직 혹은 자료구조

    - 

    ### 시간 복잡도

    - O(  )

@CMSSKKK CMSSKKK self-assigned this Oct 24, 2022
@CMSSKKK
Copy link
Member Author

CMSSKKK commented Oct 24, 2022

풀이 언어

  • java

코드

import java.io.*;

class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		StringBuilder sb = new StringBuilder();
		while (t--> 0) {
			String line = br.readLine();
			sb.append(search(line, 0, line.length() - 1 ,0)).append('\n');
		}

		System.out.println(sb);
		
	}
	
	private static int search(String s, int left, int right, int count) { 
		
		if (count == 2) { 
			return 2; 
		}
		
		int result = count; 
		while (left < right) {
			if (s.charAt(left) != s.charAt(right)) {
				result = Math.min(search(s, left + 1, right, count + 1), search(s, left, right - 1, count + 1)); 
				break; 
			} 
			left++; 
			right--; 
		} 
		
		return result; 
	}

	
}

핵심 로직 혹은 자료구조

  • 재귀
  • while문 없이 재귀로만 풀었을때, 구름에서 15번케이스부터 런타임에러 뜨길래 while문 넣고 해결했음

시간 복잡도

  • O( )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants