[programmers] rny_string - Java

๋ฌธ์ž์—ด์—์„œ 'm'์„ "rn"์œผ๋กœ ๋ฐ”๊พผ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. replaceAll() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Jan 09, 2024
[programmers] rny_string - Java

๋ฌธ์ œ ์„ค๋ช…

'm'๊ณผ "rn"์ด ๋ชจ์–‘์ด ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ธด ์ ์„ ํ™œ์šฉํ•ด ๋ฌธ์ž์—ด์— ์žฅ๋‚œ์„ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด rny_string์ด ์ฃผ์–ด์งˆ ๋•Œ, rny_string์˜ ๋ชจ๋“  'm'์„ "rn"์œผ๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 1 โ‰ค rny_string์˜ ๊ธธ์ด โ‰ค 100
  • rny_string์€ ์˜์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

rny_string
result
"masterpiece"
"rnasterpiece"
"programmers"
"prograrnrners"
"jerry"
"jerry"
"burn"
"burn"

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1
  • ์˜ˆ์ œ 1๋ฒˆ์˜ rny_string์˜ 'm'์„ "rn"์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ณผ์ •์„ ํ‘œ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    • rny_string
      m
      a
      s
      t
      e
      r
      p
      i
      e
      c
      e
      result
      rn
      a
      s
      t
      e
      r
      p
      i
      e
      c
      e
      ๋”ฐ๋ผ์„œ "rnasterpiece"๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆ #2
  • ์˜ˆ์ œ 2๋ฒˆ์˜ rny_string์˜ 'm'์„ "rn"์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ณผ์ •์„ ํ‘œ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    • rny_string
      p
      r
      o
      g
      r
      a
      m
      m
      e
      r
      s
      result
      p
      r
      o
      g
      r
      a
      rn
      rn
      e
      r
      s
      ๋”ฐ๋ผ์„œ "prograrnrners"๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆ #3
  • ์˜ˆ์ œ 3๋ฒˆ์˜ rny_string์—๋Š” 'm'์ด ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ rny_string ๊ทธ๋Œ€๋กœ์ธ "jerry"๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆ #4
  • ์˜ˆ์ œ 4๋ฒˆ์˜ rny_string์—๋Š” 'm'์ด ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ rny_string ๊ทธ๋Œ€๋กœ์ธ "burn"๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.

solution.java

class Solution { public String solution(String rny_string) { String answer = rny_string.replaceAll("m","rn"); return answer; } }
 

ํ•ต์‹ฌ ํ‚ค์›Œ๋“œ

  • replaceAll() ๋ฉ”์†Œ๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ๊ฐ’์„ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๋Œ€์ฒดํ•œ ์ƒˆ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
 

๊ฒฐ๋ก !

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ replaceAll() ๋ฉ”์†Œ๋“œ์˜ ์‚ฌ์šฉ๋ฒ•์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
 
Share article

More articles

See more posts

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ปDriedPollack's Blog