Find a word with highest number of repeating characters
Last Updated :
08 Jan, 2025
Given a string which contains multiple words, our task is to find the word which has highest number of repeating characters.
Examples:
Input: str = "hello world programming"
Output: "programming"
Explanation: The word "programming" has the highest number of repeating characters, with 'r', 'g', and 'm' each appearing twice.
Input: str= "jumping foxes swiftly"
Output: -1
Explanation: No word has any repeating character.
Note - If multiple words in the string have the highest number of repeating characters, return the one that appears first.
Using Hashing - O(n) Time and O(1) Space
The idea is to iterate each word and count the frequency of each character in the word. Finally, the word with the most repeated characters will be our result.
Step-by-step approach:
- We will use two pointers start and end, which indicates the starting and ending of each word in the given string.
- Initially, both start and end will point to the beginning of the string, and the end pointer will be incremented until a space is found, indicating the end of a word.
- Then, between the start and end pointers, we calculate the frequency of each character using an array of size 26.
- We, then count the characters with a frequency greater than 1 and compare this count to the maximum count encountered so far.
- If the current count exceeds the maximum, we will update both the maximum count and the resulting word accordingly.
C++
// C++ program to find word having most repeating characters
// Using Hashing
#include <bits/stdc++.h>
using namespace std;
string wordWithMaxRepeats(string str) {
int start = 0, end = 0;
int len = str.size();
int resStart = -1, resEnd = 0;
int maxRepeats = 0;
while(start < len) {
int curr = 0;
vector<int> freq(26, 0);
// Iterating till we reach the end of current word
// and store the frequency of all its characters
while(end < len && str[end] != ' ') {
freq[str[end] - 'a']++;
end++;
}
for(int i = 0; i < 26; i++) {
// If the current character is occuring more than once,
// it is a repeating character
if(freq[i] > 1)
curr++;
}
if(curr > maxRepeats) {
maxRepeats = curr;
resStart = start;
resEnd = end - 1;
}
// Setting start pointer at the beginning of next word
start = end + 1;
end++;
}
if(resStart == -1)
return "-1";
return str.substr(resStart , resEnd-resStart+1);
}
int main() {
string str = "hello world programming";
string ans = wordWithMaxRepeats(str);
cout << ans;
return 0;
}
C
// C program to find word having most repeating characters
// Using Hashing
#include <stdio.h>
#include <string.h>
char* wordWithMaxRepeats(char* str) {
int start = 0, end = 0;
int len = strlen(str);
int resStart = -1, resEnd = 0;
int maxRepeats = 0;
static char result[100] = "-1";
while (start < len) {
int curr = 0;
int freq[26] = {0};
// Iterating till we reach the end of current word
// and store the frequency of all its characters
while (end < len && str[end] != ' ') {
freq[str[end] - 'a']++;
end++;
}
for (int i = 0; i < 26; i++) {
// If the current character is occurring more than once,
// it is a repeating character
if (freq[i] > 1)
curr++;
}
if (curr > maxRepeats) {
maxRepeats = curr;
resStart = start;
resEnd = end - 1;
}
// Setting start pointer at the beginning of next word
start = end + 1;
end++;
}
if (resStart == -1)
return result;
strncpy(result, str + resStart, resEnd - resStart + 1);
// Null-terminate the string
result[resEnd - resStart + 1] = '\0';
return result;
}
int main() {
char str[] = "hello world programming";
char* ans = wordWithMaxRepeats(str);
printf("%s\n", ans);
return 0;
}
Java
// Java program to find word having most repeating characters
// Using Hashing
import java.util.*;
class WordWithMaxRepeats {
static String wordWithMaxRepeats(String str) {
int start = 0, end = 0;
int len = str.length();
int resStart = -1, resEnd = 0;
int maxRepeats = 0;
while (start < len) {
int curr = 0;
int[] freq = new int[26];
// Iterating till we reach the end of current word
// and store the frequency of all its characters
while (end < len && str.charAt(end) != ' ') {
freq[str.charAt(end) - 'a']++;
end++;
}
for (int i = 0; i < 26; i++) {
// If the current character is occurring more than once,
// it is a repeating character
if (freq[i] > 1)
curr++;
}
if (curr > maxRepeats) {
maxRepeats = curr;
resStart = start;
resEnd = end - 1;
}
// Setting start pointer at the beginning of next word
start = end + 1;
end++;
}
if (resStart == -1)
return "-1";
return str.substring(resStart, resEnd + 1);
}
public static void main(String[] args) {
String str = "hello world programming";
String ans = wordWithMaxRepeats(str);
System.out.println(ans);
}
}
Python
# Python program to find word having most repeating characters
# Using Hashing
def wordWithMaxRepeats(s):
start, end = 0, 0
length = len(s)
resStart, resEnd = -1, 0
maxRepeats = 0
while start < length:
curr = 0
freq = [0] * 26
# Iterating till we reach the end of current word
# and store the frequency of all its characters
while end < length and s[end] != ' ':
freq[ord(s[end]) - ord('a')] += 1
end += 1
for i in range(26):
# If the current character is occurring more than once,
# it is a repeating character
if freq[i] > 1:
curr += 1
if curr > maxRepeats:
maxRepeats = curr
resStart = start
resEnd = end - 1
# Setting start pointer at the beginning of next word
start = end + 1
end += 1
if resStart == -1:
return "-1"
return s[resStart:resEnd + 1]
str = "hello world programming"
ans = wordWithMaxRepeats(str)
print(ans)
C#
// C# program to find word having most repeating characters
// Using Hashing
using System;
public class GfG {
public static string WordWithMaxRepeats(string str) {
int start = 0, end = 0;
int len = str.Length;
int resStart = -1, resEnd = 0;
int maxRepeats = 0;
while (start < len) {
int curr = 0;
int[] freq = new int[26];
// Iterating till we reach the end of current word
// and store the frequency of all its characters
while (end < len && str[end] != ' ') {
freq[str[end] - 'a']++;
end++;
}
for (int i = 0; i < 26; i++) {
// If the current character is occurring more than once,
// it is a repeating character
if (freq[i] > 1)
curr++;
}
if (curr > maxRepeats) {
maxRepeats = curr;
resStart = start;
resEnd = end - 1;
}
// Setting start pointer at the beginning of next word
start = end + 1;
end++;
}
if (resStart == -1)
return "-1";
return str.Substring(resStart, resEnd - resStart + 1);
}
public static void Main(string[] args) {
string str = "hello world programming";
string ans = WordWithMaxRepeats(str);
Console.WriteLine(ans);
}
}
JavaScript
// JavaScript program to find word having most repeating characters
// Using Hashing
function wordWithMaxRepeats(str) {
let start = 0, end = 0;
const len = str.length;
let resStart = -1, resEnd = 0;
let maxRepeats = 0;
while (start < len) {
let curr = 0;
const freq = new Array(26).fill(0);
// Iterating till we reach the end of current word
// and store the frequency of all its characters
while (end < len && str[end] !== ' ') {
freq[str[end].charCodeAt(0) - 'a'.charCodeAt(0)]++;
end++;
}
for (let i = 0; i < 26; i++) {
// If the current character is occurring more than once,
// it is a repeating character
if (freq[i] > 1)
curr++;
}
if (curr > maxRepeats) {
maxRepeats = curr;
resStart = start;
resEnd = end - 1;
}
// Setting start pointer at the beginning of next word
start = end + 1;
end++;
}
if (resStart === -1)
return "-1";
return str.substring(resStart, resEnd + 1);
}
const str = "hello world programming";
const ans = wordWithMaxRepeats(str);
console.log(ans);
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
First non-repeating character in a stream Given an input stream s consisting solely of lowercase letters, you are required to identify which character has appeared only once in the stream up to each point. If there are multiple characters that have appeared only once, return the one that first appeared. If no character has appeared only onc
15+ min read
Find the last non repeating character in string Given a string str, the task is to find the last non-repeating character in it. For example, if the input string is "GeeksForGeeks", then the output should be 'r' and if the input string is "GeeksQuiz" then the output should be 'z'. if there is no non-repeating character then print -1.Examples: Inpu
5 min read
First non-repeating character of given string Given a string s of lowercase English letters, the task is to find the first non-repeating character. If there is no such character, return '$'.Examples: Input: s = "geeksforgeeks"Output: 'f'Explanation: 'f' is the first character in the string which does not repeat.Input: s = "racecar"Output: 'e'Ex
9 min read
Kâth Non-repeating Character in Python We need to find the first K characters in a string that do not repeat within the string. This involves identifying unique characters and their order of appearance. We are given a string s = "geeksforgeeks" we need to return the non repeating character from the string which is 'r' in this case. This
4 min read
Print Longest substring without repeating characters Given a string s having lowercase characters, find the length of the longest substring without repeating characters. Examples:Input: s = âgeeksforgeeksâOutput: 7 Explanation: The longest substrings without repeating characters are âeksforgâ and âksforgeâ, with lengths of 7.Input: s = âaaaâOutput: 1E
14 min read
Group words with same set of characters Given a list of words with lower cases. Implement a function to find all Words that have the same unique character set. Example: Input: words[] = { "may", "student", "students", "dog", "studentssess", "god", "cat", "act", "tab", "bat", "flow", "wolf", "lambs", "amy", "yam", "balms", "looped", "poodl
8 min read