[JavaScript] leetCode : Implement strStr()
728x90

๋ฌธ์ œ

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

๋‘๊ฐœ์˜ ๋ฌธ์ž์—ด needle, haystack์ด ์ฃผ์–ด์งˆ ๋•Œ, haystack์—์„œ needle์˜ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ ,
needle์ด haystack์˜ ํ•œ ๋ถ€๋ถ„์ด ์•„๋‹ˆ๋ผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

ํ’€์ด

const strStr = function(haystack, needle) {
    if (needle.length === 0) return 0;

    let index = -1;

    if (haystack.includes(needle)) {
    let firstLetter = needle.split(0, 1);
    index = haystack.indexOf(firstLetter);
    }

    return index;

};

์ƒ๊ฐํ•œ ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • haystack์ด needled์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
  • ์•„๋‹ˆ๋ผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค๋ฉด needled์˜ ์ฒซ๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ์ž˜๋ผ์˜จ๋‹ค
  • haystack์—์„œ ์ฒซ๋ฒˆ์งธ ๊ธ€์ž ์ธ๋ฑ์Šค์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

haystack์ด needle์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€๋ฅผ ๋จผ์ € ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด includes ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ–ˆ๋‹ค.
includes ๋ฉ”์„œ๋“œ๋Š” ํ•œ ๋ฌธ์ž์—ด์ด ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์— ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€๋ฅผ ํŒ๋‹จํ•ด์„œ boolean ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋‹ค.

ํฌํ•จ๋œ๋‹ค๋ฉด, split ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด needle์˜ ์ฒซ๋ฒˆ์งธ ๊ธ€์ž๋ฅผ ์ž˜๋ผ์™€์„œ
indexOf ๋ฉ”์„œ๋“œ๋กœ ์ฒซ๋ฒˆ์งธ ๊ธ€์ž์˜ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ ธ์™€ ๋ฐ˜ํ™˜ํ–ˆ๋‹ค.

 

๋‹ค๋ฅธ ํ’€์ด

const strStr = (haystack, needle) => {
  if (needle === '' || needle === haystack) return 0;    
  if (haystack.length < needle.length) return -1;        

  for (let i = 0; i < haystack.length - needle.length + 1; i++) {  
    if (haystack[i] === needle[0]) {               
      for (let j = 0; j < needle.length; j++) {     
        if (needle[j] !== haystack[i + j]) {        
          break;                                    
        } else if (j === needle.length - 1){        
          return i;                                
        }
      }
    }
  }

  return -1;                                       
};

์šฐ์„  ๋‚˜์˜ ํ’€์ด๋ณด๋‹ค ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์กฐ๊ฑด์— ๋Œ€ํ•œ ์—๋Ÿฌ์ผ€์ด์Šค๋ฅผ ๋” ์ƒ์„ธํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ–ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ ๋‚˜์˜ ๊ฒฝ์šฐ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ผ์ง€๋งŒ ์ด ํ’€์ด๋Š” for๋ฌธ๊ณผ if๋ฌธ๋งŒ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

 

์›๋ฌธ์€ ์•„๋ž˜์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

https://leetcode.com/problems/implement-strstr/discuss/590911/JavaScript-solution.-No-built-in-methods.

 

JavaScript solution. No built-in methods. - LeetCode Discuss

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

 


๐Ÿคธ‍โ™€๏ธ๐Ÿคธ‍โ™€๏ธ๐Ÿคธ‍โ™€๏ธ๐Ÿคธ‍โ™€๏ธ๐Ÿคธ‍โ™€๏ธ๐Ÿคธ‍โ™€๏ธ

๋ฐฉ๋ฌธํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๐Ÿ™Œ

ํฌ์ŠคํŒ…๋“ค์€ ๊ณต๋ถ€์ค‘์ธ ๋‚ด์šฉ์„ ๊ธ€๋กœ ์ž‘์„ฑํ•œ ๊ฒƒ์ด๋ผ ๋ถ€์กฑํ•œ ์ ์ด ๋งŽ์œผ๋‹ˆ ์ฐธ๊ณ  ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. 

๋ถ€์กฑํ•œ ๋ถ€๋ถ„์— ๋Œ€ํ•œ ์ฝ”๋ฉ˜ํŠธ๋Š” ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค.

์ข‹์€ ํ•˜๋ฃจ ๋˜์„ธ์š”, ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๐Ÿ™‚โ€‹

728x90

'๐Ÿ““ Algorithm > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JavaScript] leetCode : Single Number  (0) 2022.05.26
[JavaScript] leetCode : Reverse Words in a String  (0) 2022.05.16
[JavaScript] leetCode : Longest Common Prefix  (0) 2022.05.09
[JavaScript] leetCode : Linked List Cycle II  (3) 2022.05.03
[JavaScript] leetCode : Valid Mountain Array  (3) 2022.05.02