Repeated Character Whose First Appearance is Leftmost


Introduction

In this tutorial, we will develop an approach to finding repeated characters in a string whose first appearance is leftmost. This means the character first appeared at the beginning of the string. To find out whether the first character repeats or not, we traverse the whole string and match each character to the first character of the string. To resolve the task we use the find(), length(), and end() functions of the C++ programming language.

Example 1

String = “Tutorialspoint”
Output = The repeating character is “t”

In the above example, the leftmost character of the input string “tutorialspoint” is “t” and this character repeats in the string.

Example 2

String = “abcaabb”
Output = The repeating character is “a”

In the above example, “a” is the leftmost character of the input string and appears in other parts of the string. Hence, the output is “a”.

Example 3

String = “programming”
Output = No repeating character.

In the above example, the leftmost character of the input string is “p” and it is not repeated anywhere in the string. Hence, the output is No repeating character.

  • find() − it is a string class function returning the index value of the repeating value in the substring.

Syntax

find(char)
  • length() − it is a string class function that is used to return the length of the string.

Syntax

string_name.length()
  • end() − It is a library function that returns an iterator value of the last container element.

Algorithm

  • Take an input string of your choice.

  • Traverse the whole string and save its characters in the unordered_map.

  • Compare the first character of the string with the saved characters of the map.

  • Check if any string character matches the saved character or not.

  • Print the output.

Example

To code one of the above-listed examples, we use a brute force approach in C++ to match every character to the leftmost character in the input string. The C++ functions used in the implementation are as follows −

#include <iostream>
#include <unordered_map>

using namespace std;

char findLeftmostRepeatedChar(string s){
   unordered_map<char, int> charMap;

   // Traverse the string from left to right
   for (int x = 0; x < s.length(); x++) {
      char ch = s[x];

      // If the character is already in the map, return it
      if (charMap.find(ch) != charMap.end()) {
         return ch;
      } else {
         // Otherwise, add the character to the map
         charMap[ch] = x;
      }
   }
   // If no character is repeated, return '\0'
   return '\0';
}
int main() {
   string s = "tutorialspoint";
   char leftmostRepeatedChar = findLeftmostRepeatedChar(s);

   if (leftmostRepeatedChar != '\0'){
      cout << "The leftmost repeated character in "" << s << "" is '" << leftmostRepeatedChar << "'" << endl;
   } 
   else{
      cout << "There are no repeated characters in "" << s << """ << endl;
   }
   return 0;
}

Output

The leftmost repeated character in  << s <<  is 't'

Conclusion

In this article, we developed an approach based on C++ to find the repeating leftmost character in the input string. We use some examples to explain the meaning of the task. For the implementation of one of the examples, we used some C++ library functions. We compare the first character of the given string with all the remaining characters of the string.

Updated on: 01-Aug-2023

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements