๊ด€๋ฆฌ ๋ฉ”๋‰ด

Coding Planet

7. ํšŒ๋ฌธ ๋ฌธ์ž์—ด(Palindrome) - charAt(), StringBuilder().reverse().toString(), equals() ๋ณธ๋ฌธ

๐ŸŽ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/์•Œ๊ณ ๋ฆฌ์ฆ˜

7. ํšŒ๋ฌธ ๋ฌธ์ž์—ด(Palindrome) - charAt(), StringBuilder().reverse().toString(), equals()

jhj.sharon 2023. 5. 8. 13:30
๋ฐ˜์‘ํ˜•

1. ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ

  • ์•ž์—์„œ ์ฝ์„ ๋•Œ๋‚˜ ๋’ค์—์„œ ์ฝ์„ ๋•Œ๋‚˜ ๊ฐ™์€ ๋ฌธ์ž์—ด์„ ํšŒ๋ฌธ ๋ฌธ์ž์—ด์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ํšŒ๋ฌธ ๋ฌธ์ž์—ด์ด๋ฉด "YES", ํšŒ๋ฌธ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ฉด “NO"๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.
  • ๋‹จ ํšŒ๋ฌธ์„ ๊ฒ€์‚ฌํ•  ๋•Œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

 

 

2. ๋‚ด ํ’€์ด

  • ์ด์ „ ๋ฌธ์ œ์ธ ๋‹จ์–ด ๋’ค์ง‘๊ธฐ์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ๋กœ์ง์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ด์„œ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  • ๋‹ค๋งŒ if๋ฌธ์„ ๋งŒ๋“ค ๋•Œ '๊ฐ™์€๊ฒฝ์šฐ'์™€ '๋‹ค๋ฅธ ๊ฒฝ์šฐ' ์–ด๋–ค ๊ฒƒ์ด ๊ธฐ์ค€์ด ๋˜์–ด์•ผํ•˜๋Š”๊ฐ€ ๊ณ ๋ฏผํ•˜๋ฉด์„œ ํ—ค๋งธ๋‹ค.
  • ์‚ฌ์‹ค ๋ฐ”๊พผ๋‹ค๊ณ  ํฌ๊ฒŒ ๋‹ค๋ฅธ๊ฑด์•„๋‹ˆ์ง€๋งŒ ์–ด๋–ค ๋ณ€์ˆ˜๋ฅผ ์ฆ๊ฐ€ ๋ฐ ๊ฐ์†Œ์‹œํ‚ค๋Š”๊ฒŒ ๋’ค๋กœ ๊ฐ€๋Š”๊ฒŒ ๊น”๋”ํ•˜
import java.util.Scanner;

public class Palindrome {
	public String solution(String str) {
		
		//1) ์ „๋‹ฌ๋ฐ›์€ ๋ฌธ์ž ๋Œ€๋ฌธ์ž๋กœ ์ „ํ™˜
		String s = str.toUpperCase();
		
		//2) ๋น„๊ต๋ฅผ ์œ„ํ•ด ๋ฌธ์ž์—ด์„ ๋ฌธ์ž ๋ฐฐ์—ด๋กœ ๋ฐ”๊พธ๊ธฐ
		char[] c = s.toCharArray();
		
		//3) ํฌ์ธํ„ฐ ์„ค์ •
		int lt = 0, rt = c.length-1;
		
		//4) ๋ฐฐ์—ด์„ ๋Œ๋ฉด์„œ ์–‘๋ ๋ฌธ์ž๋ถ€ํ„ฐ ๋น„๊ตํ•˜๊ธฐ
		
			while(lt<rt) {
				if(c[lt] != c[rt]) {
					return "NO";
				}
				lt++; 
				rt--;	
			}
			return "YES";
	}

	public static void main(String[] args) {

		Palindrome T = new Palindrome();
		
		Scanner kb = new Scanner(System.in);
		String str = kb.nextLine();
		
			System.out.println(T.solution(str));
		
	}

}

 

 

3. ๋‹ค๋ฅธ ํ’€์ด(1) - charAt()

  • ํšŒ๋ฌธ์€ ์•ž์œผ๋กœ ์ฝ์œผ๋‚˜ ๋’ค๋กœ ์ฝ์œผ๋‚˜ ๋™์ผํ•˜๋ฏ€๋กœ ๋น„๊ตํ•  ๋•Œ ๋ฌธ์ž์—ด์„ ์ ˆ๋ฐ˜์œผ๋กœ ์ž˜๋ผ์„œ ๋น„๊ตํ•ด๋„ ๋œ๋‹ค(len/2)
  • ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž์™€ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ charAt์œผ๋กœ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค.

 

import java.util.Scanner;

public class Palindrome {
	public String solution(String str) {
		str=str.toUpperCase();
        int len=str.length();
        for(int = 0; i<len/2; i++){
        	if(str.charAt(i)) !=str.charAt(len-i-1)) return "NO";
		}
			return "YES";
	}

	public static void main(String[] args) {

		Palindrome T = new Palindrome();
		
		Scanner kb = new Scanner(System.in);
		String str = kb.nextLine();
		
			System.out.println(T.solution(str));
		
	}

}

 

 

4. ๋‹ค๋ฅธ ํ’€์ด(2)  - StringBuilder().reverse().toString();

  • ์•ž์„œ ์„ค๋ช…ํ•œ ๊ฒƒ ์ฒ˜๋Ÿผ ํšŒ๋ฌธ์€ ์•ž ๋’ค๋กœ ์ฝ์–ด๋„ ๋™์ผํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์ง‘์–ด๋„ ๋ฌธ์ž์—ด์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
  • ๋”ฐ๋ผ์„œ ๋’ค์ง‘์€ ๋ฌธ์ž์—ด์ด ์ฒ˜์Œ ๋ฌธ์ž์—ด๊ณผ ๋™์ผํ•ด์•ผํ•œ๋‹ค.
  • ๋ฌธ์ž์—ด์€ equals()๋กœ ๋น„๊ตํ•œ๋‹ค
import java.util.Scanner;

public class Palindrome {
	public String solution(String str) {
		
        String tmp = new StringBuilder(str).reverse().toString();
        if(str.equals(tmp)) return "YES";
        else return "No";
	}

	public static void main(String[] args) {

		Palindrome T = new Palindrome();
		
		Scanner kb = new Scanner(System.in);
		String str = kb.nextLine();
		
			System.out.println(T.solution(str));
		
	}

}
๋ฐ˜์‘ํ˜•
Comments