Skip to content

Solution of 0005 and 0006 in c languages done #4458

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

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9de9aba
olution and Readme file updated with all the terms
pranjal030404 May 23, 2025
2e3bd2f
style: format code and docs with prettier
pranjal030404 May 23, 2025
4e9aab5
Update README.md
yanglbme May 23, 2025
6b22bb9
Update README_EN.md
yanglbme May 23, 2025
bb9981e
Update Solution.c
yanglbme May 23, 2025
c05b7a5
solution of 0003 done in c
pranjal030404 May 23, 2025
55bc9c9
Merge branch 'main' of https://github.com/pranjal030404/leetcode.doocs
pranjal030404 May 23, 2025
bc3ef00
0004 done in c language
pranjal030404 May 23, 2025
1358633
Merge branch 'main' into main
pranjal030404 May 23, 2025
c154596
style: format code and docs with prettier
pranjal030404 May 23, 2025
1366d3e
Merge branch 'main' into main
pranjal030404 May 25, 2025
f609530
Update README.md
yanglbme May 26, 2025
377d9dd
Update README_EN.md
yanglbme May 26, 2025
6053870
Update Solution.c
yanglbme May 26, 2025
77e281b
Update README.md
yanglbme May 26, 2025
90bb304
Update README_EN.md
yanglbme May 26, 2025
63a213f
Update Solution.c
yanglbme May 26, 2025
c207437
0005 and 0006 done in c language
pranjal030404 Jun 3, 2025
0496362
Merge branch 'main' of https://github.com/pranjal030404/leetcode.doocs
pranjal030404 Jun 3, 2025
66176ea
Merge branch 'main' into main
pranjal030404 Jun 3, 2025
db6e8f1
style: format code and docs with prettier
pranjal030404 Jun 3, 2025
67e39b5
Merge branch 'main' into main
pranjal030404 Jun 3, 2025
7c546ef
c added
pranjal030404 Jun 3, 2025
394c41d
Merge branch 'main' of https://github.com/pranjal030404/leetcode.doocs
pranjal030404 Jun 3, 2025
f4cc2cb
style: format code and docs with prettier
pranjal030404 Jun 3, 2025
7cbf92b
formatted
pranjal030404 Jun 3, 2025
d31fb66
Merge branch 'main' of https://github.com/pranjal030404/leetcode.doocs
pranjal030404 Jun 3, 2025
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
49 changes: 49 additions & 0 deletions solution/0000-0099/0005.Longest Palindromic Substring/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,55 @@ class Solution {
}
```

#### c

```c
char *longestPalindrome(char *s) {
int n = strlen(s);
if (n == 0) {
char *result = malloc(1);
result[0] = '\0';
return result;
}

bool dp[n][n];
memset(dp, 0, sizeof(dp));

int start = 0, max_len = 1;

for (int i = 0; i < n; i++) {
dp[i][i] = true;
}

for (int i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1]) {
dp[i][i + 1] = true;
start = i;
max_len = 2;
}
}

// Check for lengths > 2
for (int len = 3; len <= n; len++) {
for (int i = 0; i < n - len + 1; i++) {
int j = i + len - 1;
if (s[i] == s[j] && dp[i + 1][j - 1]) {
dp[i][j] = true;
if (len > max_len) {
start = i;
max_len = len;
}
}
}
}
char *result = malloc(max_len + 1);
strncpy(result, s + start, max_len);
result[max_len] = '\0';
return result;
}

```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
49 changes: 49 additions & 0 deletions solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,55 @@ class Solution {
}
```

#### C

```c
char *longestPalindrome(char *s) {
int n = strlen(s);
if (n == 0) {
char *result = malloc(1);
result[0] = '\0';
return result;
}

bool dp[n][n];
memset(dp, 0, sizeof(dp));

int start = 0, max_len = 1;

for (int i = 0; i < n; i++) {
dp[i][i] = true;
}

for (int i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1]) {
dp[i][i + 1] = true;
start = i;
max_len = 2;
}
}

// Check for lengths > 2
for (int len = 3; len <= n; len++) {
for (int i = 0; i < n - len + 1; i++) {
int j = i + len - 1;
if (s[i] == s[j] && dp[i + 1][j - 1]) {
dp[i][j] = true;
if (len > max_len) {
start = i;
max_len = len;
}
}
}
}
char *result = malloc(max_len + 1);
strncpy(result, s + start, max_len);
result[max_len] = '\0';
return result;
}

```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
45 changes: 45 additions & 0 deletions solution/0000-0099/0005.Longest Palindromic Substring/Solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@


char* longestPalindrome(char* s) {
int n = strlen(s);
if (n == 0) {
char* result = malloc(1);
result[0] = '\0';
return result;
}

bool dp[n][n];
memset(dp, 0, sizeof(dp));

int start = 0, max_len = 1;

for (int i = 0; i < n; i++) {
dp[i][i] = true;
}

for (int i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1]) {
dp[i][i + 1] = true;
start = i;
max_len = 2;
}
}

// Check for lengths > 2
for (int len = 3; len <= n; len++) {
for (int i = 0; i < n - len + 1; i++) {
int j = i + len - 1;
if (s[i] == s[j] && dp[i + 1][j - 1]) {
dp[i][j] = true;
if (len > max_len) {
start = i;
max_len = len;
}
}
}
}
char* result = malloc(max_len + 1);
strncpy(result, s + start, max_len);
result[max_len] = '\0';
return result;
}
34 changes: 34 additions & 0 deletions solution/0000-0099/0006.Zigzag Conversion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,40 @@ class Solution {
}
```

#### C

```C
char *convert(char *s, int numRows) {
if (numRows == 1 || numRows >= strlen(s)) {
char *result = malloc(strlen(s) + 1);
strcpy(result, s);
return result;
}
char **rows = malloc(numRows * sizeof(char *));
for (int i = 0; i < numRows; i++) {
rows[i] = malloc(strlen(s) + 1);
rows[i][0] = '\0';
}
int currentRow = 0;
int goingDown = 0;
for (int i = 0; s[i] != '\0'; i++) {
strncat(rows[currentRow], &s[i], 1);
if (currentRow == 0 || currentRow == numRows - 1) {
goingDown = !goingDown;
}
currentRow += goingDown ? 1 : -1;
}
char *result = malloc(strlen(s) + 1);
result[0] = '\0';
for (int i = 0; i < numRows; i++) {
strcat(result, rows[i]);
free(rows[i]);
}
free(rows);
return result;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
34 changes: 34 additions & 0 deletions solution/0000-0099/0006.Zigzag Conversion/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,40 @@ class Solution {
}
```

####

```c
char *convert(char *s, int numRows) {
if (numRows == 1 || numRows >= strlen(s)) {
char *result = malloc(strlen(s) + 1);
strcpy(result, s);
return result;
}
char **rows = malloc(numRows * sizeof(char *));
for (int i = 0; i < numRows; i++) {
rows[i] = malloc(strlen(s) + 1);
rows[i][0] = '\0';
}
int currentRow = 0;
int goingDown = 0;
for (int i = 0; s[i] != '\0'; i++) {
strncat(rows[currentRow], &s[i], 1);
if (currentRow == 0 || currentRow == numRows - 1) {
goingDown = !goingDown;
}
currentRow += goingDown ? 1 : -1;
}
char *result = malloc(strlen(s) + 1);
result[0] = '\0';
for (int i = 0; i < numRows; i++) {
strcat(result, rows[i]);
free(rows[i]);
}
free(rows);
return result;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
29 changes: 29 additions & 0 deletions solution/0000-0099/0006.Zigzag Conversion/Solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
char *convert(char *s, int numRows) {
if (numRows == 1 || numRows >= strlen(s)) {
char *result = malloc(strlen(s) + 1);
strcpy(result, s);
return result;
}
char **rows = malloc(numRows * sizeof(char *));
for (int i = 0; i < numRows; i++) {
rows[i] = malloc(strlen(s) + 1);
rows[i][0] = '\0';
}
int currentRow = 0;
int goingDown = 0;
for (int i = 0; s[i] != '\0'; i++) {
strncat(rows[currentRow], &s[i], 1);
if (currentRow == 0 || currentRow == numRows - 1) {
goingDown = !goingDown;
}
currentRow += goingDown ? 1 : -1;
}
char *result = malloc(strlen(s) + 1);
result[0] = '\0';
for (int i = 0; i < numRows; i++) {
strcat(result, rows[i]);
free(rows[i]);
}
free(rows);
return result;
}