강의

멘토링

로드맵

인프런 커뮤니티 질문&답변

배우미님의 프로필 이미지
배우미

작성한 질문수

자바스크립트 알고리즘 문제풀이 입문(코딩테스트 대비)

7. 아나그램(Hash Map)

혹시 강사님 이 풀이는 효율적이지 못할까요?

작성

·

260

0

function solution(s1,s2) {
      let answer = 'YES';
      console.log(s1,s2)
      let map1 = new Map();
      for(let s of s1) {
        if(map1.has(s)) map1.set(s, map1.get(s)+1);
        else map1.set(s,1);
      }
      let map2 = new Map();
      for(let s of s2) {
        if(map2.has(s)) map2.set(s, map2.get(s)+1);
        else map2.set(s,1);
      }

      for(let s of s1) {
        if(map1.get(s) !== map2.get(s)) answer = 'NO'; break;
      }
      return answer;
    }
    let str1 = 'abaCC';
    let str2 = 'Caaab';
    console.log(solution(str1,str2))

퀴즈

What is the main reason why two-pointer or sliding window techniques are more efficient than nested loops?

Because it uses less memory?

Is it because the code is shorter?

Is it because it achieves O(N) time complexity in most cases?

Is it because it's not affected by the input data size?

답변 1

0

김태원님의 프로필 이미지
김태원
지식공유자

안녕하세요^^

잘 하신 코드입니다. 효율적으로 영상의 방법과 큰 차이가 없는 코드입니다.

배우미님의 프로필 이미지
배우미

작성한 질문수

질문하기