Open In App

Find a word with highest number of repeating characters

Last Updated : 08 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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);

Output
programming

Time Complexity: O(n)
Auxiliary Space: O(1)


Next Article
Article Tags :
Practice Tags :

Similar Reads