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

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

728x90

 

https://hyejin.tistory.com/1205

 

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

https://hyejin.tistory.com/1204 -> ์ด์ „ ๋ฌธ์ œ ํ’€์ด 9. ์ˆซ์ž๋งŒ ์ถ”์ถœ ์„ค๋ช… ๋ฌธ์ž์™€ ์ˆซ์ž๊ฐ€ ์„ž์—ฌ์žˆ๋Š” ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง€๋ฉด ๊ทธ ์ค‘ ์ˆซ์ž๋งŒ ์ถ”์ถœํ•˜์—ฌ ๊ทธ ์ˆœ์„œ๋Œ€๋กœ ์ž์—ฐ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๋งŒ์•ฝ “tge0a1h205er”์—์„œ ์ˆซ์ž

hyejin.tistory.com

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

 

 

 

9. ๊ฐ€์žฅ ์งง์€ ๋ฌธ์ž ๊ฑฐ๋ฆฌ 

 

์„ค๋ช…

ํ•œ ๊ฐœ์˜ ๋ฌธ์ž์—ด s์™€ ๋ฌธ์ž t๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๋ฌธ์ž์—ด s์˜ ๊ฐ ๋ฌธ์ž๊ฐ€ ๋ฌธ์ž t์™€ ๋–จ์–ด์ง„ ์ตœ์†Œ๊ฑฐ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— ๋ฌธ์ž์—ด s์™€ ๋ฌธ์ž t๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

 

์ถœ๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— ๊ฐ ๋ฌธ์ž์—ด s์˜ ๊ฐ ๋ฌธ์ž๊ฐ€ ๋ฌธ์ž t์™€ ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 

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

teachermode e

 

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

1 0 1 2 1 0 1 2 2 1 0

 

 

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

public int[] solution(String s, String x)
{
   int[] answer = new int[s.length()];
   Set<Integer> set = new HashSet<>();
   for (int i = 0; i< s.length(); i++)
   {
      set.add(s.indexOf(x, i));
   }
   
   ArrayList<Integer> arrayList = new ArrayList<>();
   for (int i = 0 ; i< answer.length; i++)
   {
      for (Integer integer : set)
      {
         arrayList.add(Math.abs(integer- s.indexOf(s.charAt(i), i)));
      }
      
      answer[i] =  arrayList.stream().min(Integer::compareTo).get();
      arrayList = new ArrayList<>();
   }
   
   return answer;
}

๐Ÿ‘ฉ‍๐Ÿ’ป : ๋ฏธ๋ จํ•œ ๋‚˜์˜ ํ’€์ด ๋ฐฉ๋ฒ• ... ์šฐ์„  indexOf๋ฅผ ์ด์šฉํ•ด์„œ ์ฃผ์–ด์ง„ ๋ฌธ์ž x์— ๋Œ€ํ•œ index ์œ„์น˜๋ฅผ ๋ชจ๋‘ ๊ตฌํ–ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  Set์„ ์ด์šฉํ•ด์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•ด์„œ index ์œ„์น˜๋“ค์„ ์ €์žฅํ•˜๊ณ , 

 

๊ทธ ๋‹ค์Œ, answer๋ฅผ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋„๋Š”๋ฐ set์— ์ €์žฅ๋œ index ๊ฐ’์„ ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ ์œ„์น˜์—์„œ ๋บ€ ๋‹ค์Œ (Math.abs๋ฅผ ํ•œ ์ด์œ ๋Š” ์ ˆ๋Œ€๊ฐ’์œผ๋กœ ์Œ์ˆ˜ ๊ฐ’์„ ์—†์• ๊ธฐ ์œ„ํ•ด์„œ) ๊ทธ ๊ฐ’์„ Arraylist์— ๋‹ด์•˜๊ณ , ๊ทธ ๋‹ค์Œ.. ๋บ€ ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’(๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ)์„ ๊ตฌํ•ด์„œ answer ๋ฐฐ์—ด์— ๋„ฃ์–ด์คฌ๊ณ , 

๊ทธ ๋‹ค์Œ answer๋ฅผ ๋ฆฌํ„ดํ–ˆ๋‹ค. .

(์ฐธ ํž˜๋“ค๊ฒŒ๋„ ํ’€์—ˆ๋‹ค.) 

 

 

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

    public int[] solution2(String s, char t)
   {
      int[] answer = new int[s.length()];
      int p = 1000;
      
      for (int i = 0; i < s.length(); i++)
      {
         if (s.charAt(i) == t)
         {
            p = 0;
            answer[i] = p;
         }
         else
         {
            p++;
            answer[i] = p;
         }
      }
      
      p = 1000;
      for (int i = s.length() - 1; i >= 0; i--)
      {
         if (s.charAt(i) == t)
         {
            p = 0;
         }
         else
         {
            p++;
            answer[i] = Math.min(answer[i], p);
         }
      }
      
      
      
      return answer;
   }
}

๐Ÿ‘พ : ๋จผ์ € ๊ฐ•์‚ฌ๋‹˜์€ ๋ฌธ์ž์—ด์„ ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ๋ฒˆ ๊ฐ€๊ณ , ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ํ•œ๋ฒˆ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋„๋ก ํ–ˆ๋‹ค. 

p๋Š” ์šฐ์„  ์ ๋‹นํžˆ ํฐ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค. 

 

1. ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ 

๋จผ์ € ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s ์˜ ๊ธธ์ด๋งŒํผ for๋ฌธ์„ ๋Œ๋ฉด์„œ 

s.charAt(i)๊ฐ€ ์ฃผ์–ด์ง„ ๋ฌธ์ž t ์™€ ๊ฐ™์œผ๋ฉด p ์—๋Š” 0์œผ๋กœ ์„ค์ •ํ•œ ๋‹ค์Œ answer ๋ฐฐ์—ด์— p ๊ฐ’์„ ์ €์žฅํ–ˆ๋‹ค. (p๊ฐ€ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—์„œ ํ•ด๋‹น ๋ฌธ์ž์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๋œ๋‹ค.) 

 

๊ทธ๋ฆฌ๊ณ  t์™€ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด p ์—์„œ +1 ์„ ํ•ด์ฃผ๊ณ , answer ๋ฐฐ์—ด์— p ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. 

๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์™ผ์ชฝ t ๊ฐ’์— ๋Œ€ํ•œ ๋ฌธ์ž ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๊ฒŒ ๋œ๋‹ค. 

for (int i = 0; i < s.length(); i++)
{
   if (s.charAt(i) == t)
   {
      p = 0;
      answer[i] = p;
   }
   else
   {
      p++;
      answer[i] = p;
   }
}

 

2. ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ 

๊ทธ ๋‹ค์Œ์—” ์ด์ œ ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์ฃผ์–ด์ง„ ๋ฌธ์ž t์™€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด์„œ answer ๊ฐ’์— ๋„ฃ์–ด์ฃผ๋Š” ๋ฐ ์ด๋•Œ ๊ธฐ์กด์— ๋“ค์–ด์žˆ๋˜ ๊ฐ’๋ณด๋‹ค ๊ฐ’์ด ์ ์€ ๊ฒฝ์šฐ์—๋งŒ p ๊ฐ’์„ answer ๋ฐฐ์—ด์— ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. 

p = 1000;
for (int i = s.length() - 1; i >= 0; i--)
{
   if (s.charAt(i) == t)
   {
      p = 0;
   }
   else
   {
      p++;
      answer[i] = Math.min(answer[i], p);
   }
}

 

๐Ÿ‘ฉ‍๐Ÿ’ป : ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์„ ๋†”๋‘๊ณ  ์—„์ฒญ ๋น™๋น™ ๋Œ๋ ค์„œ ๋ฌธ์ œ๋ฅผ ํ‘ผ ๊ฒƒ ๊ฐ™๋‹ค... ์ด ๋ฐฉ์‹ ๊ธฐ์–ตํ•ด๋‘ฌ์•ผ์ง€.. 

 

 

 

 

 

 

 

 

 

 

 

 

728x90