Develop
2017.06.27 14:38
[coding] Find all anagrams in a string
조회 수 3850 댓글 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);
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
1129 | Develop | [android] AlertDialog 메시지 창 띄우기 | hooni | 2015.07.09 | 2053 |
1128 | Develop |
GPL, AGPL, MPL,.. 한눈에 보는 오픈소스SW 라이선스
![]() |
hooni | 2014.10.14 | 2062 |
1127 | Develop | [js] 스크롤 이벤트 막기 | hooni | 2015.04.14 | 2070 |
1126 | Develop | [ios] Thread Loop 내에서 UI 업데이트 방법 | hooni | 2015.01.03 | 2072 |
1125 | Develop |
[c#] mfc 기반의 웹서비스 서버/클라이언트 샘플과 예제 소스
![]() |
hooni | 2013.04.23 | 2073 |
1124 | Develop | [linux] CentOS Apache 웹서버에 HTTPS 적용 | hooni | 2015.10.23 | 2085 |
1123 | Develop |
[c#] MS IE(Internet Explorer) 툴바 버튼 예제 2003/2005 두가지 버전
![]() |
hooni | 2013.04.23 | 2090 |
1122 | Develop |
[maven] Mac OS에 메이븐(maven) 설치하기
![]() |
hooni | 2015.01.21 | 2090 |
1121 | Develop |
정리할 자료.
![]() |
hooni | 2015.07.02 | 2092 |
1120 | Develop |
XE Core 1.8.18 본문 작성시 태그(html) 사라지는 버그
![]() |
hooni | 2016.04.21 | 2102 |
1119 | Develop |
[android] N-Puzzle 게임
![]() |
hooni | 2015.07.09 | 2112 |
1118 | Develop | [ios] ViewController Push할 때 애니메이션 효과 | hooni | 2015.10.23 | 2114 |