Develop
2017.06.27 14:38
[coding] Find all anagrams in a string
조회 수 3861 댓글 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);
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
1153 | System/OS |
[펌] 마이크로서비스, 모노리포, SRE, ... 덮어놓고 구글 따라하면 안 되는 기술들
![]() |
hooni | 2020.10.15 | 3358 |
1152 | Develop | [js] Text 값을 클립보드에 복사하기 | hooni | 2020.10.10 | 2742 |
1151 | System/OS | Apache CORS 설정 1 | hooni | 2020.09.04 | 6146 |
1150 | System/OS |
How to Install and Use wget on Mac
![]() |
hooni | 2020.09.03 | 5598 |
1149 | System/OS |
[mac] VirtualBox 실행 스크립트와 bash_profile 설정
![]() |
hooni | 2020.07.08 | 3350 |
1148 | System/OS | [linux] wget 명령 사용 예제 | hooni | 2020.05.26 | 4203 |
1147 | System/OS | [linux] The Ultimate Wget Download Guide With 15 Awesome Examples | hooni | 2020.05.26 | 3771 |
1146 | Develop | [sh] 쉘스크립트 if 비교 연산 | hooni | 2020.05.26 | 64385 |
1145 | Develop |
[sh] html 안에 있는 img 다운 받는 쉘 스크립트
![]() |
hooni | 2020.05.26 | 2573 |
1144 | Develop |
[ios] Start developing your navigation app for CarPlay without enrollment
![]() |
hooni | 2020.02.22 | 132881 |
1143 | System/OS |
Configure Postfix to Use Gmail SMTP on Ubuntu 18.04
![]() |
hooni | 2020.02.07 | 12311 |
1142 | System/OS |
RPA란? 어디에 어떻게 쓰이고 누가 만드나?
![]() |
hooni | 2020.01.28 | 3217 |