Develop
2017.06.27 14:38
[coding] Find all anagrams in a string
Views 1140 Comment 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);
No. | Category | Subject | Author | Date | Views |
---|---|---|---|---|---|
35 | PPT | [ajax] 크로스 도메인(Cross Domain) 이슈 해결 방안 | hooni | 2013.04.23 | 21937 |
34 | PPT | [android] XML파서(XMLParser)의 종류와 성능 비교에 대한 발표 자료 | hooni | 2013.04.23 | 75640 |
33 | PPT | [Android] 제스처 자료.. | hooni | 2013.05.28 | 51072 |
32 | PPT | [android][ios] 알림(Notification) 기능에 대한 원리와 구현 방안 (APNS포함) | hooni | 2013.04.23 | 37851 |
31 | PPT | [doc] BHO관련 발표 자료(디펜스에 쓸꺼) | hooni | 2013.05.28 | 0 |
30 | PPT | [doc] 논문.. VoIP 관련.. ㅋㅋ | hooni | 2013.04.23 | 17079 |
29 | PPT | [doc] 발표 자료 ㅎㅎtalk4neo | hooni | 2013.04.23 | 20987 |
28 | PPT | [doc] 방송통신공학회 논문지(학술대회) | hooni | 2013.04.23 | 13924 |
27 | PPT | [doc] 위탁과제 제안서와 발표자료(pdf) | hooni | 2013.04.23 | 12534 |
26 | PPT | [doc] 인공지능 관련 자료(채팅로봇도 포함..) | hooni | 2013.04.23 | 18001 |
25 | PPT | [doc] 정보보호이론 강의자료 (중앙대꺼..) 2 | hooni | 2013.04.23 | 22811 |
24 | PPT | [doc] 정보보호학술대회 논문지 ㅋㅋ | hooni | 2013.04.23 | 12178 |