Develop
2017.06.27 14:38
[coding] Find all anagrams in a string
조회 수 1309 댓글 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);
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
213 | System/OS | CentOS 에서 Cacti 설치하기 | hooni | 2015.01.02 | 1947 |
212 | Develop | [ios] Xcode cannot run using the selected device | hooni | 2014.08.14 | 1931 |
211 | Etc | 인증서 *.p12 파일을 *.pem 파일로 변환 | hooni | 2015.04.30 | 1919 |
210 | System/OS | [linux] CentOS 에 APM 설치하기 | hooni | 2015.01.02 | 1918 |
209 | Develop | [php] mysql_ 과 mysqli_ 의 차이 | hooni | 2017.12.01 | 1865 |
208 | Develop | [python][django] request.cookie 읽어오기 ㅋㅋㅋ (쓰기) | hooni | 2019.12.06 | 1850 |
207 | Develop | Laravel 5 Failed opening required bootstrap/../vendor/autoload.php | hooni | 2018.01.24 | 1847 |
206 | Develop | [ios] Pod 특정 버전 설치하고 사용하기 | hooni | 2022.05.28 | 1833 |
205 | System/OS | [svn] 하나의 SVN에서 멀티 저장소 (One svnserve, multiple repositories) | hooni | 2015.01.02 | 1801 |
204 | Develop | [c] 한글 문자열 출력 | hooni | 2015.11.10 | 1799 |
203 | System/OS | 네트워크 용어 정리 | hooni | 2022.11.20 | 1790 |
202 | Develop | [ios] @property의 속성 (strong, weak, copy) 사용 경우 | hooni | 2014.08.08 | 1783 |