문제
다음 두 가지 작업을 지원하는 JavaScript의 데이터 구조를 설계해야 합니다.
- 데이터 구조(DS)에 단어를 추가하는 addWord를 사용하면 배열이나 다른 DS와 같은 기존 DS를 사용하여 이 데이터를 저장할 수 있습니다.
- 검색, 소문자 "a-z" 또는 "."가 포함된 리터럴 단어 또는 정규식 문자열을 검색합니다. 어디 "." 모든 문자를 나타낼 수 있습니다.
예를 들어
addWord("sir")
addWord("car")
addWord("mad")
search("hell") === false
search(".ad") === true
search("s..") === true 예시
다음은 코드입니다 -
class MyData{
constructor(){
this.arr = [];
};
};
MyData.prototype.addWord = function (word) {
this.arr.push(word)
};
MyData.prototype.search = function (word) {
let reg = new RegExp('^'+word+'$');
return !!this.arr.find(el => reg.test(el));
};
const data = new MyData();
data.addWord('sir');
data.addWord('car');
data.addWord('mad');
console.log(data.search('hell'));
console.log(data.search('.ad'));
console.log(data.search('s..')); 출력
다음은 콘솔 출력입니다 -
false true true