์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜1. String (๋ฌธ์ž์—ด) : ํšŒ๋ฌธ ๋ฌธ์ž์—ด

2023. 10. 6. 10:00ใ†์ธํ”„๋Ÿฐ/์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„

728x90

https://hyejin.tistory.com/1202

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜1. String (๋ฌธ์ž์—ด) : ์ค‘๋ณต๋ฌธ์ž์ œ๊ฑฐ

https://hyejin.tistory.com/1201 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜1. String (๋ฌธ์ž์—ด) : ํŠน์ • ๋ฌธ์ž ๋’ค์ง‘๊ธฐ https://hyejin.tistory.com/1200 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜1. String (๋ฌธ์ž์—ด) : ๋‹จ์–ด ๋’ค์ง‘๊ธฐ https:/

hyejin.tistory.com

-> ์ด์ „ ๋ฌธ์ œ ํ’€์ด 

 

7. ํšŒ๋ฌธ ๋ฌธ์ž์—ด 

 
 

์„ค๋ช…

์•ž์—์„œ ์ฝ์„ ๋•Œ๋‚˜ ๋’ค์—์„œ ์ฝ์„ ๋•Œ๋‚˜ ๊ฐ™์€ ๋ฌธ์ž์—ด์„ ํšŒ๋ฌธ ๋ฌธ์ž์—ด์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ํšŒ๋ฌธ ๋ฌธ์ž์—ด์ด๋ฉด "YES", ํšŒ๋ฌธ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ฉด “NO"๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๋‹จ ํšŒ๋ฌธ์„ ๊ฒ€์‚ฌํ•  ๋•Œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์ž…๋ ฅ

์ฒซ ์ค„์— ๊ธธ์ด 100์„ ๋„˜์ง€ ์•Š๋Š” ๊ณต๋ฐฑ์ด ์—†๋Š” ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— ํšŒ๋ฌธ ๋ฌธ์ž์—ด์ธ์ง€์˜ ๊ฒฐ๊ณผ๋ฅผ YES ๋˜๋Š” NO๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ ์ž…๋ ฅ 1 

gooG

 

์˜ˆ์‹œ ์ถœ๋ ฅ 1

YES

 

 

 

๋ฌธ์ œ ํ’€์ด 1

public String solution(String input)
{
   input = input.toLowerCase();
   String reverseInput = new StringBuilder(input).reverse().toString();
   
    return (input.equals(reverseInput)) ?  "YES" :"NO";
}

๐Ÿ‘ฉ‍๐Ÿ’ป : ํšŒ๋ฌธ๋ฌธ์ž์—ด์ด๋ž€ ๋ฌธ์ž์—ด์„ ์•ž์—์„œ ์ฝ์œผ๋‚˜ ๋’ค์—์„œ ์ฝ์œผ๋‚˜ ๋™์ผํ•œ ๋ฌธ์ž์—ด์„ ๋งํ•œ๋‹ค. 

๊ทผ๋ฐ ์—ฌ๊ธฐ์„œ ๋Œ€์†Œ๋ฌธ์ž๋Š” ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ธฐ๋กœ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฒˆ์—๋Š” toUpperCase๋ฅผ ํ†ตํ•ด ์šฐ์„  ๋ฌธ์ž์—ด์„ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ํ†ต์ผํ•ด์คฌ๋‹ค. 

๊ทธ ๋‹ค์Œ ์˜ˆ์ „ StringBuilder์˜ reverse ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋˜ ๊ฒƒ์„ ํ™œ์šฉํ•ด์„œ reverseInput๊ณผ input์ด ๋™์ผํ•˜๋ฉด YES๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ํ–ˆ๋‹ค. 

 

 

 

๋ฌธ์ œ ํ’€์ด 2

public String solution2(String input)
{
   String answer = "YES";
   int length = input.length();
   input = input.toUpperCase();
   
   for (int i = 0; i< length/2; i++)
   {
      if (input.charAt(i) != input.charAt(length - i - 1)) {
         return "NO";
      }
   }
   
   return answer;
}

๐Ÿ‘พ : ๊ฐ•์‚ฌ๋‹˜์€ ์šฐ์„  charAt์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ ํ’€์ด๋ฅผ ์ง„ํ–‰ํ•˜์…จ๋‹ค. 

๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ ์„œ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ ๋•Œ๋„ ๊ทธ ์ ˆ๋ฐ˜๊นŒ์ง€๋งŒ ๋Œ๋ฉด์„œ ์ ˆ๋ฐ˜์˜ ์•ž๋ถ€๋ถ„๊ณผ ๋’ท๋ถ€๋ถ„์„ ์„œ๋กœ ๋น„๊ตํ•˜๋ฉด ๋œ๋‹ค. 

๋”ฐ๋ผ์„œ input.charAt(i)๊ฐ€ input.charAt(length - i - 1) ๊ณผ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด ํšŒ๋ฌธ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ฏ€๋กœ ๊ทธ ์ฆ‰์‹œ NO๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. 

(์ด๋•Œ length - i - 1 ์„ ํ•œ ์ด์œ ๋Š” ๋ฌธ์ž์—ด์˜ ๋งจ ๋์˜ ์œ„์น˜๋Š” length- 1 ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

 

๋ฌธ์ œ ํ’€์ด 3

public String solution3(String input)
{
   String answer = "NO";
   String tmp = new StringBuilder(input).reverse().toString();
   if (input.equalsIgnoreCase(tmp)) {
      return "YES";
   }
   
   return answer;
}

๐Ÿ‘พ : ์ด๋ฒˆ ํ’€์ด๋Š” ๋‚ด๊ฐ€ ํ’€์—ˆ๋˜ ๋ฐฉ์‹๊ณผ ๋™์ผํ•˜๊ฒŒ StringBuilder๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์—ฌ๊ธฐ์„œ ๋‹ค๋ฅธ ์ ์ด๋ผ๋ฉด ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„œ ๋จผ์ € toUpperCase๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์—ด์„ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋Œ€์‹  ๋น„๊ตํ•  ๋•Œ equals๊ฐ€ ์•„๋‹Œ equalsIgnoreCase๋ฅผ ์ด์šฉํ•ด์„œ ๋Œ€์†Œ๋ฌธ์ž ์ƒ๊ด€์—†์ด ๋น„๊ต๋ฅผ ํ–ˆ๋‹ค. 

 

 

 

 

 

 

 

 

 

728x90