VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-a-word-with-highest-number-of-repeating-characters/

⇱ Find a word with highest number of repeating characters - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find a word with highest number of repeating characters

Last Updated : 8 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.

Output
programming

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

Comment
Article Tags:
Article Tags: