Develop
2017.06.27 14:38
[coding] Find all anagrams in a string
조회 수 1306 댓글 0
코딩테스트 관련 URL
https://leetcode.com/problems/find-all-anagrams-in-a-string/#/description
# 내가 푼 내용
/** * @param {string} s * @param {string} p * @return {number[]} */ var findAnagrams = function(s, p) { var res = Array(); if(s.length < p.length){ return res; } p1 = Array.from(p).sort().join(""); for(var i=0; i<=s.length-p.length; i++){ var s1 = s.substring(i, p.length + i); s1 = Array.from(s1).sort().join(""); if(s1 == p1){ res.push(i); } } return res; }; var s = "cbaebabacd"; var p = "abc"; var r = findAnagrams(s, p); console.log(r);
# 봐도 모르는 넘사벽 정답을 그냥 Javascript로 변경만 한거 ㅋㅋ
/** * @param {string} s * @param {string} p * @return {number[]} */ var findAnagrams = function(s, p) { var res = Array(); if(s.length < p.length){ return res; } var hash = new Array(256).fill(0); var left = 0; var right = 0; var count = p.length; for(var i=0; i<p.length; i++){ //hash[p.charCodeAt(i)] = (hash[p.charCodeAt(i)] || 0) + 1; hash[p.charCodeAt(i)]++; console.log("hash["+p.charCodeAt(i)+"] "+hash[p.charCodeAt(i)]); } while(right < s.length){ if (hash[s.charCodeAt(right++)]-- >= 1){ count--; } if(count == 0){ res.push(left); } if(right - left == p.length && hash[s.charCodeAt(left++)]++ >= 0){ count++; } } return res; }; var s = "cbaebabacd"; var p = "abc"; var r = findAnagrams(s, p); console.log(r);
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
685 | Develop | [c++] 헤더, 소스 파일 분리해서 작성해본 테스트 소스 | hooni | 2013.04.23 | 6719 |
684 | Develop | [c++] 현승이가 준 P2P 프로그램 소스 ㅋㅋ | hooni | 2013.04.23 | 12107 |
683 | Develop | [c++] 현승이가 준 메신저 소스.. ㅋㅋ | hooni | 2013.04.23 | 8301 |
682 | Develop | [c++][mfc] 파일 입출력 샘플 (한줄씩 읽어서 다른 파일에 쓰기) | hooni | 2013.04.23 | 15063 |
681 | Develop | [c++]현승이가 보내준 동영상 암호화 자료.. 볼것.. | hooni | 2003.04.23 | 9759 |
680 | Develop | [c++]현승이가 보내준 암호화 모듈 AES라인델.. | hooni | 2003.04.23 | 10745 |
679 | Develop | [c/c++] Makefile 사용하기.. ㅋㅋ | hooni | 2003.04.23 | 7392 |
678 | Develop | [chm] C++ 문법 가이드 | hooni | 2013.04.23 | 7971 |
677 | Develop | [chm] Programming Applications for Microsoft Windows | hooni | 2013.04.23 | 7287 |
676 | Develop | [chm] 비주얼 C++ 팁 모음 문서 | hooni | 2013.04.23 | 7521 |
675 | Develop | [chm] 윈도우즈에서 디버깅 기법(Debugging Applications) | hooni | 2013.04.23 | 7553 |
» | Develop | [coding] Find all anagrams in a string | hooni | 2017.06.27 | 1306 |