mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
problem 3
Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
This commit is contained in:
parent
f28213c3ee
commit
24154d890b
@ -1,12 +1,33 @@
|
|||||||
int lengthOfLongestSubstring(char *str)
|
/**
|
||||||
|
* \file
|
||||||
|
* \brief [3. Longest
|
||||||
|
* substring](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
|
||||||
|
* optimized solution
|
||||||
|
* \details Given a string `s`, find the length of the longest substring without
|
||||||
|
* repeating characters.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Optimized Longest substring function.
|
||||||
|
*
|
||||||
|
* @param str NULL terminated alpha-numeric string to search
|
||||||
|
* @return length of longest substring
|
||||||
|
* @note no spaces or other non-alphanumeric characters
|
||||||
|
*/
|
||||||
|
int lengthOfLongestSubstring(const char *str)
|
||||||
{
|
{
|
||||||
int n = strlen(str);
|
size_t n = strlen(str);
|
||||||
|
|
||||||
if (!n)
|
if (!n)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
int L_len = 1; // lenght of longest substring
|
int L_len = 1; // length of longest substring
|
||||||
int C_len = 1; // lenght of current substring
|
int C_len = 1; // length of current substring
|
||||||
|
|
||||||
int P_ind, i; // P_ind for previous index
|
int P_ind, i; // P_ind for previous index
|
||||||
int visited[256]; // visited will keep track of visiting char for the last
|
int visited[256]; // visited will keep track of visiting char for the last
|
||||||
@ -33,33 +54,25 @@ int lengthOfLongestSubstring(char *str)
|
|||||||
L_len = C_len;
|
L_len = C_len;
|
||||||
return L_len;
|
return L_len;
|
||||||
}
|
}
|
||||||
/* Brute force */
|
|
||||||
int lengthOfLongestSubstring(char *s)
|
|
||||||
{
|
|
||||||
int cur_max = 0, max = 0;
|
|
||||||
int counter[255];
|
|
||||||
int end = 0;
|
|
||||||
|
|
||||||
memset(counter, 0, sizeof(int) * 255);
|
/**
|
||||||
while (end < strlen(s))
|
* @brief Main function
|
||||||
{
|
* @return 0
|
||||||
if (counter[s[end]] == 0)
|
*/
|
||||||
{
|
int main()
|
||||||
counter[s[end]]++;
|
{
|
||||||
end++;
|
int test1 = lengthOfLongestSubstring("abcabcbb");
|
||||||
cur_max++;
|
assert(test1 == 3);
|
||||||
}
|
printf("Test 1 passed\n");
|
||||||
else
|
int test2 = lengthOfLongestSubstring("bbbbb");
|
||||||
{
|
assert(test2 == 1);
|
||||||
char c = s[end];
|
printf("Test 2 passed\n");
|
||||||
memset(counter, 0, 255 * sizeof(int));
|
int test3 = lengthOfLongestSubstring("pwwkew");
|
||||||
if (cur_max >= max)
|
assert(test3 == 3);
|
||||||
max = cur_max;
|
printf("Test 3 passed\n");
|
||||||
cur_max = 0;
|
int test4 = lengthOfLongestSubstring("");
|
||||||
while (s[end - 1] != c) end--;
|
assert(test4 == 0);
|
||||||
}
|
printf("Test 4 passed\n");
|
||||||
}
|
|
||||||
if (cur_max >= max)
|
return 0;
|
||||||
max = cur_max;
|
|
||||||
return max;
|
|
||||||
}
|
}
|
||||||
|
72
leetcode/problem_3/sol2.c
Normal file
72
leetcode/problem_3/sol2.c
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
/**
|
||||||
|
* \file
|
||||||
|
* \brief [3. Longest
|
||||||
|
* substring](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
|
||||||
|
* brute force solution
|
||||||
|
* \details Given a string `s`, find the length of the longest substring without
|
||||||
|
* repeating characters.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Optimized Longest substring function.
|
||||||
|
*
|
||||||
|
* @param str NULL terminated alpha-numeric string to search
|
||||||
|
* @return length of longest substring
|
||||||
|
* @note no spaces or other non-alphanumeric characters
|
||||||
|
*/
|
||||||
|
int lengthOfLongestSubstring(const char *s)
|
||||||
|
{
|
||||||
|
int cur_max = 0, max = 0;
|
||||||
|
int counter[255];
|
||||||
|
int end = 0;
|
||||||
|
|
||||||
|
memset(counter, 0, sizeof(int) * 255);
|
||||||
|
while (end < strlen(s))
|
||||||
|
{
|
||||||
|
if (counter[s[end]] == 0)
|
||||||
|
{
|
||||||
|
counter[s[end]]++;
|
||||||
|
end++;
|
||||||
|
cur_max++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char c = s[end];
|
||||||
|
memset(counter, 0, 255 * sizeof(int));
|
||||||
|
if (cur_max >= max)
|
||||||
|
max = cur_max;
|
||||||
|
cur_max = 0;
|
||||||
|
while (s[end - 1] != c) end--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (cur_max >= max)
|
||||||
|
max = cur_max;
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Main function
|
||||||
|
* @return 0
|
||||||
|
*/
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int test1 = lengthOfLongestSubstring("abcabcbb");
|
||||||
|
assert(test1 == 3);
|
||||||
|
printf("Test 1 passed\n");
|
||||||
|
int test2 = lengthOfLongestSubstring("bbbbb");
|
||||||
|
assert(test2 == 1);
|
||||||
|
printf("Test 2 passed\n");
|
||||||
|
int test3 = lengthOfLongestSubstring("pwwkew");
|
||||||
|
assert(test3 == 3);
|
||||||
|
printf("Test 3 passed\n");
|
||||||
|
int test4 = lengthOfLongestSubstring("");
|
||||||
|
assert(test4 == 0);
|
||||||
|
printf("Test 4 passed\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user