Develop
2017.06.27 14:38
[coding] Find all anagrams in a string
조회 수 1123 댓글 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);
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
1149 | System/OS | [mac] VirtualBox 실행 스크립트와 bash_profile 설정 | hooni | 2020.07.08 | 925 |
1148 | System/OS | [linux] wget 명령 사용 예제 | hooni | 2020.05.26 | 1412 |
1147 | System/OS | [linux] The Ultimate Wget Download Guide With 15 Awesome Examples | hooni | 2020.05.26 | 777 |
1146 | Develop | [sh] 쉘스크립트 if 비교 연산 | hooni | 2020.05.26 | 58969 |
1145 | Develop | [sh] html 안에 있는 img 다운 받는 쉘 스크립트 | hooni | 2020.05.26 | 621 |
1144 | Develop | [ios] Start developing your navigation app for CarPlay without enrollment | hooni | 2020.02.22 | 124540 |
1143 | System/OS | Configure Postfix to Use Gmail SMTP on Ubuntu 18.04 | hooni | 2020.02.07 | 1226 |
1142 | System/OS | RPA란? 어디에 어떻게 쓰이고 누가 만드나? | hooni | 2020.01.28 | 1338 |
1141 | Develop | [php] 3 Ways to Detect Mobile or Desktop in PHP | hooni | 2020.01.28 | 3704 |
1140 | Develop | 자주 쓰는 Docker 명령어 alias | hooni | 2020.01.10 | 268367 |
1139 | Develop | [python][django] request.cookie 읽어오기 ㅋㅋㅋ (쓰기) | hooni | 2019.12.06 | 1671 |
1138 | System/OS | SSH Passwordless Login Using SSH Keygen in 5 Easy Steps | hooni | 2019.11.22 | 1374 |