문제요약
word는 영어 소문자와 숫자의 조합으로 이루어져있다word에서 숫자부분만 추출한 이후 서로 다른 정수의 개수를 반환한다
예시
- 입력: word = "a 123 bc 34 d 8 ef 34 "
출력: 3 - 입력: word = "leet 1234 code 234 "
출력: 2 - 입력: word = "a 1 b 01 c 001 "
출력: 1
풀이
var numDifferentIntegers = function(word) {
const regex = /[0-9]+/g;
const arr = word.match(regex)
if(!arr) return 0
const set = new Set(arr.map(n => BigInt(n)))
return [...set].length
};- 정규 표현식을 사용하여 정수부분만 추출
- 해당 정수부분을 BigInt로 변환하며 Set에 저장
- Set의 길이를 반환
시간복잡도 : O(n)
'코딩테스트' 카테고리의 다른 글
| LeetCode - Set Matrix Zeroes (1) | 2025.01.02 |
|---|---|
| LeetCode - Minimum Add to Make Parentheses Valid (1) | 2025.01.02 |
| LeetCode - Restore IP Addresses (0) | 2024.12.28 |
| LeetCode - Path Sum (0) | 2024.12.25 |
| LeetCode - Maximum Average Subarray 1 (1) | 2024.12.23 |