Develop
2017.06.27 14:38
[coding] Find all anagrams in a string
조회 수 1144 댓글 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);
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
1041 | System/OS | Enable the Develop Menu in Safari | hooni | 2017.02.07 | 1077 |
1040 | Develop | [ios] FlckrFeed Example App (Swift) | hooni | 2016.11.27 | 1079 |
1039 | PPT | [ppt] 정보보호관리 발표내용 | hooni | 2016.11.30 | 1081 |
1038 | Etc | 원어민이 매일 쓰는 일상표현 150개 | hooni | 2023.09.17 | 1096 |
1037 | Develop | [js] 문자열에서 숫자만 걸러내기 (jQuery 안쓰고 정규표현식) | hooni | 2017.12.14 | 1108 |
1036 | Develop | [maven] Mac OS에 메이븐(maven) 설치하기 | hooni | 2015.01.21 | 1110 |
1035 | Etc | 영어. 반드시 외워야 할 문장 패턴 100개 | hooni | 2023.06.08 | 1113 |
1034 | Develop | [ios] UIView 계층구조 | hooni | 2015.01.03 | 1128 |
» | Develop | [coding] Find all anagrams in a string | hooni | 2017.06.27 | 1144 |
1032 | Develop | GPL, AGPL, MPL,.. 한눈에 보는 오픈소스SW 라이선스 | hooni | 2014.10.14 | 1146 |
1031 | Develop | macOS에 node, npm 설치하기 (homebrew) | hooni | 2021.11.06 | 1148 |
1030 | System/OS | Enable Safari Hidden Debug Menu in Mac OS X | hooni | 2017.02.07 | 1151 |