Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- dacon
- Kaggle
- programmers
- 자연어처리
- ubuntu
- 프로그래머스 파이썬
- Git
- 파이썬
- PYTHON
- leetcode
- Real or Not? NLP with Disaster Tweets
- 데이콘
- ChatGPT
- 코로나19
- 백준
- 편스토랑
- gs25
- SW Expert Academy
- 우분투
- 더현대서울 맛집
- 금융문자분석경진대회
- 편스토랑 우승상품
- 프로그래머스
- Baekjoon
- hackerrank
- github
- Docker
- 캐치카페
- 맥북
- AI 경진대회
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 3. Longest Substring Without Repeating Characters (Python) 본문
Programming/코딩 1일 1문제
[leetCode] 3. Longest Substring Without Repeating Characters (Python)
솜씨좋은장씨 2020. 5. 11. 17:05728x90
반응형
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
Solution
class Solution:
def lengthOfLongestSubstring(self, my_str: str) -> int:
answer = 0
substrings = []
my_str_list = list(my_str)
set_list = set(my_str_list)
if len(set_list) == 0:
answer = 0
elif len(set_list) == 1:
answer = 1
else:
for i in range(len(my_str_list)):
sub = []
for j in range(i, len(my_str_list)):
if my_str_list[j] in sub:
substring = ''.join(sub)
substrings.append(len(substring))
break
sub.append(my_str_list[j])
if j == len(my_str_list) - 1:
substring = ''.join(sub)
substrings.append(len(substring))
answer = max(substrings)
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 6. ZigZag Conversion (Python) (0) | 2020.05.13 |
---|---|
[BaekJoon] 1764번 : 듣보잡 (Python) (0) | 2020.05.12 |
[BaekJoon] 11053번 : 가장 긴 증가하는 부분수열 (Python) (0) | 2020.05.10 |
[BaekJoon] 11655번 : ROT13 (Python) (0) | 2020.05.09 |
[BaekJoon] 1406번 : 에디터 (Python) (0) | 2020.05.08 |
Comments