Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Longest_Substring_Without_Repeating_Characters.cpp #12

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>

Check notice on line 1 in Solved-Problems/Longest_Substring_Without_Repeating_Characters/Longest_Substring_Without_Repeating_Characters.cpp

View workflow job for this annotation

GitHub Actions / c-cpp-linter / c-cpp-linter

Run clang-format on Solved-Problems/Longest_Substring_Without_Repeating_Characters/Longest_Substring_Without_Repeating_Characters.cpp

File Solved-Problems/Longest_Substring_Without_Repeating_Characters/Longest_Substring_Without_Repeating_Characters.cpp does not conform to Custom style guidelines. (lines 1, 6, 7, 8, 9, 10, 12, 13, 15, 16, 17, 18, 20, 21, 23, 24, 25, 29, 30, 31, 32, 34, 35, 44, 49, 55)
#include <unordered_map>
#include <string>
using namespace std;

// Function to find the length of the longest substring without repeating characters
int lengthOfLongestSubstring(const string &s) {
unordered_map<char, int> charIndexMap;
int maxLength = 0;
int start = 0;

for (int end = 0; end < s.length(); end++) {
char currentChar = s[end];

// If the character is already in the map and is within the current window
if (charIndexMap.find(currentChar) != charIndexMap.end() && charIndexMap[currentChar] >= start) {
start = charIndexMap[currentChar] + 1;
}

// Update the last seen index of the current character
charIndexMap[currentChar] = end;

// Calculate the max length of substring
maxLength = max(maxLength, end - start + 1);
}
return maxLength;
}

int main() {
string input;
cout << "Enter a string: ";
cin >> input;

int result = lengthOfLongestSubstring(input);
cout << "The length of the longest substring without repeating characters is: " << result << endl;

return 0;
}

/*
Example:
Input: "abcabcbb"
Explanation:
- The longest substring without repeating characters is "abc", which has a length of 3.
- As we iterate through the string:
- Start with "a" -> length 1
- Add "b" -> "ab" -> length 2
- Add "c" -> "abc" -> length 3
- "a" repeats, so move the start to the next character -> "bca" (length still 3)
- Continue until the end, with the longest substring remaining "abc".
Output: 3

Input: "bbbbb"
Explanation:
- The longest substring without repeating characters is "b", which has a length of 1.
Output: 1
*/
RohithaAiswarya16 marked this conversation as resolved.
Show resolved Hide resolved
Loading