🧠 Top 5 String Problems in C

🧠 Problem 1: Count Palindromic Substrings

Problem Statement:
Given a string S, find how many distinct substrings of S are palindromes.

đŸ“Ĩ Input:

A single line containing string S (1 ≤ |S| ≤ 1000).

📤 Output:

Print a single integer — the number of palindromic substrings.

🔍 Sample Input:

ababa

✅ Sample Output:

5

💡 Output Explanation:

The palindromic substrings are:
a, b, aba, bab, ababa
Total = 5

🧑‍đŸ’ģ C Starter Code:


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool isPalindrome(char *str, int l, int r) {
    while (l < r) {
        if (str[l++] != str[r--]) return false;
    }
    return true;
}

int main() {
    char str[1001];
    scanf("%s", str);
    int len = strlen(str);
    int count = 0;

    for (int i = 0; i < len; i++) {
        for (int j = i; j < len; j++) {
            if (isPalindrome(str, i, j)) count++;
        }
    }

    printf("%d\n", count);
    return 0;
}

🔡 Problem 2: Group Words by Anagram

Problem Statement: Group N strings into sets of anagrams.

đŸ“Ĩ Input:

First line: Integer N (1 ≤ N ≤ 1000)
Next N lines: One string per line

📤 Output:

Number of anagram groups.

🔍 Sample Input:

5
listen
silent
enlist
google
gogole

✅ Sample Output:

2

💡 Output Explanation:

Group 1: listen, silent, enlist
Group 2: google, gogole
Total = 2 groups

🧑‍đŸ’ģ C Starter Code:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void sortString(char *str) {
    int len = strlen(str);
    for (int i = 0; i < len-1; i++) {
        for (int j = i+1; j < len; j++) {
            if (str[i] > str[j]) {
                char t = str[i];
                str[i] = str[j];
                str[j] = t;
            }
        }
    }
}

int main() {
    int n;
    scanf("%d", &n);
    char words[1000][101], sorted[1000][101];
    int group = 0, visited[1000] = {0};

    for (int i = 0; i < n; i++) {
        scanf("%s", words[i]);
        strcpy(sorted[i], words[i]);
        sortString(sorted[i]);
    }

    for (int i = 0; i < n; i++) {
        if (visited[i]) continue;
        group++;
        for (int j = i+1; j < n; j++) {
            if (!visited[j] && strcmp(sorted[i], sorted[j]) == 0)
                visited[j] = 1;
        }
    }

    printf("%d\n", group);
    return 0;
}

🔍 Problem 3: Longest Unique Substring

Problem Statement: Find the length of the longest substring without repeating characters.

đŸ“Ĩ Input:

A string S (1 ≤ |S| ≤ 105)

📤 Output:

Length of longest unique substring.

🔍 Sample Input:

abcabcbb

✅ Sample Output:

3

💡 Output Explanation:

Longest substrings without repeats:
abc, bca, cab
Length = 3

🧑‍đŸ’ģ C Starter Code:


#include <stdio.h>
#include <string.h>

int main() {
    char str[100001];
    scanf("%s", str);
    int last[256], maxLen = 0, start = 0;

    for (int i = 0; i < 256; i++) last[i] = -1;

    for (int i = 0; str[i]; i++) {
        if (last[(int)str[i]] >= start)
            start = last[(int)str[i]] + 1;
        last[(int)str[i]] = i;
        if (i - start + 1 > maxLen)
            maxLen = i - start + 1;
    }

    printf("%d\n", maxLen);
    return 0;
}

đŸ§Ē Problem 4: Count Pattern Occurrences

Problem Statement: Count how many times pattern P occurs in string S (including overlaps).

đŸ“Ĩ Input:

Line 1: S (1 ≤ |S| ≤ 106)
Line 2: P (1 ≤ |P| ≤ 104)

📤 Output:

Number of occurrences.

🔍 Sample Input:

aaaaa
aaa

✅ Sample Output:

3

💡 Output Explanation:

"aaa" found at:
Index 0 → aaa
Index 1 → aaa
Index 2 → aaa
Total = 3

🧑‍đŸ’ģ C Starter Code:


#include <stdio.h>
#include <string.h>

int main() {
    char text[1000001], pattern[10001];
    scanf("%s %s", text, pattern);
    int count = 0, lenT = strlen(text), lenP = strlen(pattern);

    for (int i = 0; i <= lenT - lenP; i++) {
        if (strncmp(&text[i], pattern, lenP) == 0)
            count++;
    }

    printf("%d\n", count);
    return 0;
}

đŸ§Ŧ Problem 5: Lexicographically Smallest Rotation

Problem Statement: Print the smallest lexicographical rotation of a string.

đŸ“Ĩ Input:

One line: a string S (1 ≤ |S| ≤ 1000)

📤 Output:

Smallest rotation of the string.

🔍 Sample Input:

baca

✅ Sample Output:

acab

💡 Output Explanation:

All rotations:
baca, acab, caba, abac
Sorted:
abac, acab, baca, caba
Smallest = acab

🧑‍đŸ’ģ C Starter Code:


#include <stdio.h>
#include <string.h>

int main() {
    char str[2001], temp[1001], min[1001];
    scanf("%s", temp);
    int len = strlen(temp);

    strcpy(min, temp);
    strcpy(str, temp);
    strcat(str, temp); // simulate circular shift

    for (int i = 1; i < len; i++) {
        char sub[1001];
        strncpy(sub, str + i, len);
        sub[len] = '\0';
        if (strcmp(sub, min) < 0)
            strcpy(min, sub);
    }

    printf("%s\n", min);
    return 0;
}

āĻ•োāύ āĻŽāύ্āϤāĻŦ্āϝ āύেāχ:

āĻāĻ•āϟি āĻŽāύ্āϤāĻŦ্āϝ āĻĒোāϏ্āϟ āĻ•āϰুāύ