[go: up one dir, main page]

0% found this document useful (0 votes)
67 views2 pages

Longest Substring Without Repeation

Uploaded by

HASMUKH RUSHABH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views2 pages

Longest Substring Without Repeation

Uploaded by

HASMUKH RUSHABH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

brute force approach is that take two for loop and go contionous until the longest
string but it will not repeat any string value

Sliding techinque is used for optimal solution by using two pointer solution
left and right pointer => move the right pointer until you find the repeating value
and also update the max value of string

We can use set that it doesnt take duplicate values and the time complexity is the
O(n) and aslo spacecomplexity O(n)

class Solution {
public int lengthOfLongestSubstring(String s) {
int left = 0 ;
int right = 0;
// here i have created the set

Set<Character> seen = new HashSet();


// max integer is used to count the max value substring in the string
without any repeat

int max = 0;
// we will move the pointer unntil that right pointer does'nt finish the
char

while (right < s.length() ){


// this is right pointer

char c = s.charAt(right);
// this if loop tells that it will add the character until it will not
repaet

if (seen.add(c)){
// here the max value is update and it is comapared with the
perivous value

max = Math.max(max,right-left+1);
}else {

// here the else loop is used for repeating value until it is not
removed
while(s.charAt(left) != c){
seen.remove(s.charAt(left));
left++;
}
seen.remove(c);
left++;
}

}
return max;
}
}

class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> map = new HashMap<>();
int i = 0;
int j = 0;
int max = 0;
while(j < s.length()){
map.put(s.charAt(j), map.getOrDefault(s.charAt(j), 0) + 1);
if(map.size() == j - i + 1){
max = Math.max(max, j - i + 1);
j++;
}
else if(map.size() < j - i + 1){
while(map.size() < j - i + 1){
map.put(s.charAt(i), map.get(s.charAt(i)) - 1);
if(map.get(s.charAt(i)) == 0) map.remove(s.charAt(i));
i++;
}
j++;
}
}
return max;
}
}

class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> map = new HashMap<>();
int i = 0;
int j = 0;
int max = 0;
while(j < s.length()){
// s.charAt() is used for the finding the index of
// map.put(key , value)

map.put(s.charAt(j) , map.getOrDefault(s.charAt(j), 0) + 1);


if(map.size() == j - i + 1){
max = Math.max(max, j - i + 1);
j++;
}
else if(map.size() < j - i + 1){
while(map.size() < j - i + 1){
map.put(s.charAt(i), map.get(s.charAt(i)) - 1);
if(map.get(s.charAt(i)) == 0) map.remove(s.charAt(i));
i++;
}
j++;
}
}
return max;
}
}

You might also like