problem 3

Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
This commit is contained in:
Krishna Vedala 2020-10-30 02:35:58 -04:00
parent f28213c3ee
commit 24154d890b
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7
2 changed files with 117 additions and 32 deletions

View File

@ -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)
return 0;
int L_len = 1; // lenght of longest substring
int C_len = 1; // lenght of current substring
int L_len = 1; // length of longest substring
int C_len = 1; // length of current substring
int P_ind, i; // P_ind for previous index
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;
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
*/
int main()
{
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;
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;
}

72
leetcode/problem_3/sol2.c Normal file
View 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;
}