đ§ 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;
}
āĻোāύ āĻŽāύ্āϤāĻŦ্āϝ āύেāĻ:
āĻāĻāĻি āĻŽāύ্āϤāĻŦ্āϝ āĻĒোāϏ্āĻ āĻāϰুāύ