Develop
2017.06.27 14:38

[coding] Find all anagrams in a string

조회 수 3850 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

코딩테스트 관련 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);


?

  1. [c] Unix Domain Socket 을 이용한 IPC

    Date2013.04.23 CategoryDevelop Byhooni Views9953
    Read More
  2. [c] UTF-8을 EUC-KR로 변환.. (iconv)

    Date2013.04.23 CategoryDevelop Byhooni Views21279
    Read More
  3. [c] vc++ 에서 clrscr(), gotoxy() 함수 사용하기..

    Date2013.04.23 CategoryDevelop Byhooni Views15248
    Read More
  4. [c] 가변인자 함수(printf와 같은..)

    Date2013.04.23 CategoryDevelop Byhooni Views8363
    Read More
  5. [c] 가위 바위 보 서버, 클라이언트 소스코드

    Date2003.04.23 CategoryDevelop Byhooni Views9330
    Read More
  6. [c] 간단한 링크드 리스트(linked list) 자료형 예제..

    Date2003.04.23 CategoryDevelop Byhooni Views11885
    Read More
  7. [c] 간단한 소켓 프로그래밍 샘플

    Date2013.04.23 CategoryDevelop Byhooni Views9492
    Read More
  8. [c] 간단한 순위 루틴.. (정보처리기사)

    Date2003.04.23 CategoryDevelop Byhooni Views8483
    Read More
  9. [c] 간단한 순위 루틴.. (질문에 대한 답변)

    Date2003.04.23 CategoryDevelop Byhooni Views8496
    Read More
  10. [c] 간단한 자료구조(stack, queue, linked list) 구현 소스

    Date2003.04.23 CategoryDevelop Byhooni Views11496
    Read More
  11. [c] 간단한 점 이동 샘플 소스코드

    Date2013.04.23 CategoryDevelop Byhooni Views7983
    Read More
  12. [c] 간단한 채팅(클라이언트/서버) 프로그램 소스

    Date2003.04.23 CategoryDevelop Byhooni Views10404
    Read More
  13. [c] 거리와 각도를 입력받아서 좌표로 변환

    Date2013.04.23 CategoryDevelop Byhooni Views11454
    Read More
  14. [c] 게임 AI FSM 테스트 샘플 소스.. 꽤 괜찮은 소스..

    Date2013.04.23 CategoryDevelop Byhooni Views8248
    Read More
  15. [c] 격자 직사각형 넓이 구하기

    Date2013.04.23 CategoryDevelop Byhooni Views8687
    Read More
  16. [c] 공용체를 이용해 MSB를 LSB로 변환

    Date2013.04.23 CategoryDevelop Byhooni Views10091
    Read More
Board Pagination Prev 1 ... 12 13 14 15 16 ... 53 Next
/ 53