forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Longest_substring_without_repeating_chars.cpp
76 lines (60 loc) · 1.8 KB
/
Longest_substring_without_repeating_chars.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* This code is submitted to NeoAlgo as a part of Contributors hack 2020
Finding the longest substring without repeating characters having linear time Complexity O(n) and constant Space Complexity O(1) */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdbool.h>
int Lols(char str[], int len) //length of longest substring
{
if (str==NULL || len==0) //returning zero if the string is empty
{
return 0;
}
int i=0; // varible for traversing in the string
int j=0; // varible for traversing in the string
int max_len=0; // to store the maximum length of the substring w/o repeating Characters
bool hash[256]={0}; // boolean varible to check for the charecters
while(i<len)
{
if (hash[str[i]])
{
if (i-j+1>max_len)
{
max_len=i-j+1; //max_len=max(max_len,i-j+1);
}
while(str[j]!=str[i])
{
hash[str[j]]=0;
j++;
}
i++;j++;
}
else
{
hash[str[i]]=1;
i++;
}
}
if (max_len<len-j)
{
max_len=len-j; //max_len=max(max_len,len-j);
}
return max_len-1;
}
int main(void)
{
char string[20];
printf("Enter Input String:");
scanf("%[^\n]%*c", string);
int len=strlen(string); // length of the string
printf("\nlength of longest substring without repeating characters: %d\n", Lols(string,len));
}
/* Sample Input-output
Input: Enter Input String:abcbacbb
Output: length of longest substring without repeating charecters: 3
Input: Enter Input String:Hackincodes
Output: length of longest substring without repeating charecters: 7
Input: Enter Input String:ddddd
Output: length of longest substring without repeating charecters: 1
Input: Enter Input String:
Output: length of longest substring without repeating charecters: 0 */