Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Maximum number of characters between any two same character in a string in C
We are given a string of alphabets. The array can have at least two occurrences of the same character. The task here is to find the maximum number of characters between any two occurrences of a character. If there is no repetition of any character then return -1.
Input − string str = "abcdba"
Output − Maximum number of characters between any two same character in a string − 4
Explanation − The repeating characters are 'a' and 'b' only with indexes −
1. 'a' first index 0 last 5, characters in between 5-0-1=4 2. 'b' first index 1 last 4, characters in between 4-1-1=2 Maximum character in between repeating alphabets: 4
Syntax
int maxChars(char str[], int n);
Approach
- We take a character array having a string of characters as
str[] - The function
maxChars(char str[], int n)calculates the maximum number of characters between any two repeating characters - We initialize the variable
maxCwith -1 - Using nested loops, we compare each character with all subsequent characters
- If a match is found, calculate the difference between indexes using
j-i-1 - Keep track of the maximum difference found
- Return
maxCafter traversing the whole string
Example
#include <stdio.h>
int maxChars(char str[], int n) {
int maxC = -1;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (str[i] == str[j]) {
int temp = j - i - 1;
if (temp > maxC) {
maxC = temp;
}
}
}
}
return maxC;
}
int main() {
char str[] = "AbcAaBcbC";
int len = 9;
printf("String: %s<br>", str);
int result = maxChars(str, len);
if (result == -1) {
printf("No repeating characters found<br>");
} else {
printf("Maximum number of characters between any two same character: %d<br>", result);
}
return 0;
}
String: AbcAaBcbC Maximum number of characters between any two same character: 5
Example with No Repeating Characters
#include <stdio.h>
int maxChars(char str[], int n) {
int maxC = -1;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (str[i] == str[j]) {
int temp = j - i - 1;
if (temp > maxC) {
maxC = temp;
}
}
}
}
return maxC;
}
int main() {
char str[] = "abcdefg";
int len = 7;
printf("String: %s<br>", str);
int result = maxChars(str, len);
if (result == -1) {
printf("No repeating characters found<br>");
} else {
printf("Maximum number of characters between any two same character: %d<br>", result);
}
return 0;
}
String: abcdefg No repeating characters found
How It Works
The algorithm uses a brute force approach with O(n²) time complexity. For the string "AbcAaBcbC":
- 'A' appears at positions 0 and 3 − characters between: 3-0-1 = 2
- 'b' appears at positions 1 and 7 − characters between: 7-1-1 = 5
- 'c' appears at positions 2 and 6 − characters between: 6-2-1 = 3
The maximum is 5, which is returned as the result.
Conclusion
This approach effectively finds the maximum distance between any two occurrences of the same character in a string. The function returns -1 when no character repeats, making it useful for string analysis problems.
