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

Longest Substring in Java #2087

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion 2D_Array/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ Due to the fact that the elements of 2D arrays can be random accessed. Similar t
* Checking Teoplitz Matrix Algorithm ----> [Java](/Code/Java/Toeplitz.java)
* Clockwise Rotation ----> [C++](/Code/C++/2d_matrix_rotation_90degree_clockwise.cpp)
* Inverse of a Matrix ----> [Java](/Code/Java/matrixop_inverse.java)
* Longest Substring Without Repeating Characters ----> [Java](/Code/Java/Longest_Substring.java)
* Matrix Operations ----> [C++](/Code/C++/matrix_operations.cpp)
* Multiplication of two Matrices ----> [Java](/Code/Java/matrixop_mul.java)
* Overall Median of Matrix ---> [Java](/Code/Java/overall_median_matrix.java)
* Row-wise sum ----> [C++](/Code/C++/row_wise_sum.cpp) | [java](/Code/java/RowWise_Sum.java)
* Spiral Print ----> [C++](/Code/C++/spiral_print.cpp)
* Staircase Search ----> [C++](/Code/C++/staircase_search.cpp)
* Substraction of two Matrices ----> [Java](/Code/Java/matrixop_sub.java)
* Transpose of a Matrix --->[Java](/Code/Java/transpose.java) | [Python](/Code/Python/Transpose_of_matrix.py)
* Transpose of a Matrix --->[Java](/Code/Java/transpose.java) | [](/Code//Transpose_of_matrix.py)
* Wave Print ----> [C++](/Code/C++/wave_print.cpp)
47 changes: 47 additions & 0 deletions Code/Java/Longest_Substring.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Question

// 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.
// Example 4:

// Input: s = ""
// Output: 0

// *****************************************************************************************************


// Solution

public class Longest Substring {
public int lengthOfLongestSubstring(String s) {
int len = s.length();
int ans=0;
Map<Character,Integer> map = new HashMap<>();
for(int j=0, i=0; j<len; j++){
if(map.containsKey(s.charAt(j)))
i = .max(map.get(s.charAt(j)), i);
ans = .max(ans, j-i+1);
map.put(s.charAt(j), j+1);
}
return ans;
}
}

// Time Complexity = O(n)
// Space Complexity = O(n)