leetcode.com/problems/jewels-and-stones/

 

Jewels and Stones - LeetCode

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

javascript

/**
 * @param {string} J
 * @param {string} S
 * @return {number}
 */
const numJewelsInStones = function (J, S) {
  const stons = [...S]
  let numJewelsInStone = 0
  for (let i = 0; i < stons.length; i++) {
    if (J.includes(stons[i])) {
      numJewelsInStone++
    }
  }
  return numJewelsInStone
};

typescript

const numJewelsInStonesTS = function (J: string, S: string): number {
  const stons: string[] = [...S]
  let numJewelsInStone: number = 0
  for (let i = 0; i < stons.length; i++) {
    if (J.includes(stons[i])) {
      numJewelsInStone++
    }
  }
  return numJewelsInStone
};

golang

func numJewelsInStones(J string, S string) int {
	jewels := make(map[rune]bool)
	for _, jewel := range J {
		jewels[jewel] = true
	}
	numJewelsInStone := 0
	for _, stone := range S {
		if jewels[stone] {
			numJewelsInStone++
		}
	}
	return numJewelsInStone
}

/*
func numJewelsInStones(J string, S string) int {
	num := 0
	for _, jewel := range J {
		for _, stone := range S {
			if jewel == stone {
				num++
			}
		}
	}
	return num
}
*/

 

'코딩 테스트 > LeetCode' 카테고리의 다른 글

Easy) 1512. Number of Good Pairs  (0) 2020.09.15
Easy) 1108. Defanging an IP Address  (0) 2020.09.15
Easy) Shuffle the Array  (0) 2020.09.13
Easy) Kids With the Greatest Number of Candies  (0) 2020.09.13
Easy. Running Sum of 1d Array  (0) 2020.07.02

+ Recent posts