Gyanthakur / LeetCode_potd

16 stars 34 forks source link

Create potd_11_10_2024.py #26

Closed Faizalimam990 closed 1 week ago

Faizalimam990 commented 1 week ago

Longest Substring Without Repeating Characters

Problem Statement

This pull request introduces the problem statement for the "Longest Substring Without Repeating Characters" challenge. The objective is to find the length of the longest substring without repeating characters in a given string s.

Examples

  1. Input: s = "abcabcbb"
    Output: 3
    Explanation: The longest substring is "abc", with the length of 3.

  2. Input: s = "bbbbb"
    Output: 1
    Explanation: The longest substring is "b", with the length of 1.

  3. Input: s = "pwwkew"
    Output: 3
    Explanation: The longest substring is "wke", with the length of 3.

Constraints

Solution

This solution employs the sliding window technique to efficiently find the length of the longest substring without repeating characters.

Implementation


def length_of_longest_substring(s: str) -> int:
    char_set = set()
    left = 0
    max_length = 0

    for right in range(len(s)):
        while s[right] in char_set:
            char_set.remove(s[left])
            left += 1
        char_set.add(s[right])
        max_length = max(max_length, right - left + 1)

    return max_length
## Fixes: #[issue_number] (replace with the issue number, if applicable)

## Type of Change

- [x] Question Added
- [x] Solution Added
- [ ] Other (please specify):

## How to Test

To test the changes, follow these steps:

1. Clone the repository.
2. Run the provided code in a Python environment.
3. Test with various input strings to ensure correctness.

## Checklist

- [ ] I have performed a self-review of my code.
- [ ] I have commented my code, particularly in hard-to-understand areas.
- [ ] I have made corresponding changes to the documentation (if applicable).
- [ ] My changes generate no new warnings.
- [ ] I have added tests to cover my changes (if applicable).
- [ ] All new and existing tests pass.
Gyanthakur commented 1 week ago

Longest Substring Without Repeating Characters

Problem Statement

This pull request introduces the problem statement for the "Longest Substring Without Repeating Characters" challenge. The objective is to find the length of the longest substring without repeating characters in a given string s.

Examples

  1. Input: s = "abcabcbb" Output: 3 Explanation: The longest substring is "abc", with the length of 3.
  2. Input: s = "bbbbb" Output: 1 Explanation: The longest substring is "b", with the length of 1.
  3. Input: s = "pwwkew" Output: 3 Explanation: The longest substring is "wke", with the length of 3.

Constraints

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

Solution

This solution employs the sliding window technique to efficiently find the length of the longest substring without repeating characters.

Implementation

def length_of_longest_substring(s: str) -> int:
    char_set = set()
    left = 0
    max_length = 0

    for right in range(len(s)):
        while s[right] in char_set:
            char_set.remove(s[left])
            left += 1
        char_set.add(s[right])
        max_length = max(max_length, right - left + 1)

    return max_length
## Fixes: #[issue_number] (replace with the issue number, if applicable)

## Type of Change

- [x] Question Added
- [x] Solution Added
- [ ] Other (please specify):

## How to Test

To test the changes, follow these steps:

1. Clone the repository.
2. Run the provided code in a Python environment.
3. Test with various input strings to ensure correctness.

## Checklist

- [ ] I have performed a self-review of my code.
- [ ] I have commented my code, particularly in hard-to-understand areas.
- [ ] I have made corresponding changes to the documentation (if applicable).
- [ ] My changes generate no new warnings.
- [ ] I have added tests to cover my changes (if applicable).
- [ ] All new and existing tests pass.

you are not following my account.