LeetCode Challenge (Python3). Problem#3

logo_dark.e99485d9b.png

Source

Introduction of this challenge:

I recently decided to improve my programming skills, so I started using LeetCode to achieve this goal.

However, since I don't have much programming experience and this is my first time using LeetCode, there may be some difficulties in the process, but I believe I will be able to persevere.

There are currently 2230 problems on LeetCode, and I will be working on the challenge by problem number with the goal of solving all of them.

After solving problem 2, I spent a few days solving problem 3. For me, problem 3 is much easier than problem 2, and one of the main reasons I think is that python is much easier and faster than other programming languages in terms of data processing, so I don't think it's a technical improvement for me, I think it's just a language advantage.

So basically I only spent some time solving the problem3, part of which was spent on understanding python's string processing methods, and the rest on thinking about the logic part.


Today's problem is "3. Longest Substring Without Repeating Characters".

Link: https://leetcode.com/problems/longest-substring-without-repeating-characters/
Difficulty: Medium
Related Topics: Hash Table String Sliding Window

Problem:

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.

My solution:

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        ss=''
        i=0  

        for c in range(len(s)):
            if s[c] not in ss: # Determine if a character is a duplicate
                ss=ss+s[c]
                if c==len(s)-1 and len(ss) > i: # Determine if it is the last character and if the length of the substring is the longest 
                    i=len(ss)
            elif len(ss) > i: # Determines if the length of the substring is the longest when the character is repeated
                i=len(ss)
                nc=s.rfind(s[c],0,c) # Find the last occurrence of duplicate characters
                ss=s[nc+1:c+1] # Let the substring be the content between the previous duplicate character and the duplicate character
            else:
                nc=s.rfind(s[c],0,c)
                ss=s[nc+1:c+1]
        return i

After coding, I submitted my solution.

_2022_04_21_1.26.43.png

Memory usage seems to be more than most, and I think this will be the focus of my future rethinking of the solution, and of course speed needs to be improved.


This is the end of the leetcode challenge.

Feel free to give some suggestions based on my code, whether it's a way to improve performance or shorten the code.

Thanks for stopping by here and reading my post.

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center