항해99 2주차 - 중복문자가 없는 가장 긴 부분 문자열

Jang Seok Woo·2022년 1월 23일
0

알고리즘

목록 보기
22/74

Today I learned
2022/01/19

회고록


1/19

항해 99, 알고리즘 1주차

교재 : 파이썬 알고리즘 인터뷰

10장 해시테이블

1. 이론

https://velog.io/@jsw4215/%ED%95%AD%ED%95%B499-2%EC%A3%BC%EC%B0%A8-%EB%B3%B4%EC%84%9D%EA%B3%BC-%EB%8F%8C

2. 문제

Given a string s, find the length of the longest substring without repeating characters.

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

Constraints:

0 <= s.length <= 5 * 10^4
s consists of English letters, digits, symbols and spaces.

https://leetcode.com/problems/longest-substring-without-repeating-characters/

3. MySol

def solution(strs):
    result = {}

    for i in range(len(strs)):
        result[1] = strs[i]
        for j in range(i+1,len(strs)):
            temp= strs[i:j]
            if strs[j] not in temp:
                if j-i+1 not in result:
                    result[j-i+1] = strs[i:j+1]
            else:
                break

    ansKey=max(result.keys())
    ret=result[ansKey]

    return ansKey


if __name__ == '__main__':

    strs = 'abcabcbb'

    result = solution(strs)

    print('result : ' + str(result))

4. 배운 점

  • 반복문을 이용하여 풀었으나, 해당 문제는 투 포인터를 이용하여 풀 수 있었다.
  • 어찌보면 내 것도 투 포인터긴 한데.. 효율성을 위해선 투 포인터는 다른 규칙적인 로직을 더 효율적으로 활용할 수 있는 로직이 필수적이다.

5. 총평

해시 테이블 훈련

profile
https://github.com/jsw4215

0개의 댓글