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

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

728x90

https://hyejin.tistory.com/1200

 

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

https://hyejin.tistory.com/1199 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜1. String (๋ฌธ์ž์—ด) : ๋ฌธ์žฅ ์† ๋‹จ์–ด https://hyejin.tistory.com/1198 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜1. String (๋ฌธ์ž์—ด) : ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜ https://hy

hyejin.tistory.com

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

 

 

5. ํŠน์ • ๋ฌธ์ž ๋’ค์ง‘๊ธฐ

 

์„ค๋ช…

์˜์–ด ์•ŒํŒŒ๋ฒณ๊ณผ ํŠน์ˆ˜๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง€๋ฉด ์˜์–ด ์•ŒํŒŒ๋ฒณ๋งŒ ๋’ค์ง‘๊ณ ,

ํŠน์ˆ˜๋ฌธ์ž๋Š” ์ž๊ธฐ ์ž๋ฆฌ์— ๊ทธ๋Œ€๋กœ ์žˆ๋Š” ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์–ด ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ž…๋ ฅ

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

 

์ถœ๋ ฅ

์ฒซ ์ค„์— ์•ŒํŒŒ๋ฒณ๋งŒ ๋’ค์ง‘ํžŒ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

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

a#b!GE*T@S

 

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

S#T!EG*b@a

 

 

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

public String solution(String input)
{
   char[] chars = input.toCharArray();
   int lt = 0, rt = chars.length-1;
   
   while (lt < rt)
   {
      if ((chars[lt] >= 'A' && chars[lt] <= 'z') && (chars[rt] >= 'A' && chars[rt] <= 'z')) {
         char tmp = chars[lt];
         chars[lt] = chars[rt];
         chars[rt] = tmp;
         lt++;
         rt--;
      }else if ((chars[lt] >= 'A' && chars[lt] <= 'z') && !(chars[rt] >= 'A' && chars[rt] <= 'z')){
         rt --;
      }
      else if (!(chars[lt] >= 'A' && chars[lt] <= 'z') && (chars[rt] >= 'A' && chars[rt] <= 'z')) {
         lt++;
      }else {
         lt++;
         rt--;
      }
   }
   
   return String.valueOf(chars);
}

๐Ÿ‘ฉ‍๐Ÿ’ป : ํŠน์ • ๋‹จ์–ด ๋’ค์ง‘๋Š” ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์ €๋ฒˆ ๋ฌธ์ œ ํ’€์ด์˜ ์‘์šฉ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. 

๋จผ์ € lt์™€ rt ๋กœ ์™ผ์ชฝ ๋ถ€ํ„ฐ, ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ํ•ด์„œ ์ ์  ๊ฐ€์šด๋ฐ๋กœ ๋ชจ์ด๋Š” ์›€์ง์ด๋ฉด์„œ ์„œ๋กœ์˜ ๋‹จ์–ด๋ฅผ ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค. 

๊ทผ๋ฐ ์ด์ œ ์—ฌ๊ธฐ์„œ ํŠน์ˆ˜ ๋ฌธ์ž๋Š” ๊ทธ๋Œ€๋กœ ๋‘ฌ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํŠน์ˆ˜ ๋ฌธ์ž๊ฐ€ ์•„๋‹Œ ์•ŒํŒŒ๋ฒณ๋งŒ ์›€์ง์—ฌ์ฃผ๋ฉด ๋œ๋‹ค. 

 

๋งŒ์•ฝ lt๊ฐ€ ํŠน์ˆ˜๋ฌธ์ž๋ผ๋ฉด lt ++ ํ•ด์ฃผ๋ฉด ๋˜๊ณ , rt ๊ฐ€ ํŠน์ˆ˜๋ฌธ์ž๋ผ๋ฉด rt -- ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ๋‘˜ ๋‹ค ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๊ทธ๋•Œ ๋‘ ๋ฌธ์ž๋ฅผ ๊ตํ™˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

ํŠน์ˆ˜ ๋ฌธ์ž๊ฐ€ ์•„๋‹˜์„ ํŒ๋‹จํ•˜๋Š”๋ฐ ๋น„๊ต์‹์ด ๋„ˆ๋ฌด ๋”๋Ÿฝ๋‹ค.... ์•„ ... ๐Ÿ˜‚

 

 

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

public String solution2(String input)
{
   char[] chars = input.toCharArray();
   int lt = 0, rt = chars.length - 1;
   
   while (lt < rt)
   {
      if (!Character.isAlphabetic(chars[lt])) lt++;
      else if (!Character.isAlphabetic(chars[rt])) rt--;
      else {
         char tmp = chars[lt];
         chars[lt] = chars[rt];
         chars[rt] = tmp;
         lt++;
         rt --;
      }
   }
   
   return String.valueOf(chars);
}

๐Ÿ‘พ : ๋ฌธ์ œ ํ’€์ด ๋ฐฉ์‹์€ ๋™์ผํ•œ๋ฐ ํ›จ์”ฌ ๊น”๋”ํ•ด๋ณด์ด๋Š” ์ด์œ ๋Š” ์กฐ๊ฑด์‹์„ ์ž˜ ์ •๋ฆฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ.. ๊ทธ๋ฆฌ๊ณ  Character.isAlpahbetic ์ด๋ž€ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ๋ชฐ๋ž๋‹ค..!! ์ด๊ฒƒ๋งŒ ์•Œ์•˜์–ด๋„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋Š”๋ฐ..!!! 

Character.isAlpahbetic ์„ ํ†ตํ•ด chars[lt]์˜ ๊ฐ’์ด ์•ŒํŒŒ๋ฒณ์ด ์•„๋‹ˆ๋ผ๋ฉด lt์˜ ์œ„์น˜๋ฅผ ++1 ํ•ด์ค€๋‹ค. 

๋˜๋Š” Character.isAlpahbetic์„ ํ†ตํ•ด chars[rt]์˜ ๊ฐ’์ด ์•ŒํŒŒ๋ฒณ์ด ์•„๋‹ˆ๋ผ๋ฉด rt์˜ ์œ„์น˜๋ฅผ --1 ํ•ด์ค€๋‹ค.

 

์ด๋ ‡๊ฒŒ ์œ„์น˜๋ฅผ ์˜ฎ๊ฒผ๋”๋‹ˆ ์•ŒํŒŒ๋ฒณ์ด๋ผ๋ฉด ๊ทธ๋•Œ  ๋‘ ๋‹จ์–ด๋ฅผ ๊ตํ™˜ํ•ด์ฃผ๊ณ , lt ๋Š” +1, rt๋Š” -1 ํ•ด์ค€๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ๋ฆฌํ„ด ํƒ€์ž…์€ String์ด๊ธฐ ๋•Œ๋ฌธ์— String.valueOf()๋ฅผ ํ†ตํ•ด ๋ฌธ์ž ๋ฐฐ์—ด์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋ฆฌํ„ดํ•ด์ค€๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90