Home > Backend Development > C++ > body text

Repeating characters whose first occurrence is on the far left

PHPz
Release: 2023-08-31 18:05:05
forward
1010 people have browsed it

Repeating characters whose first occurrence is on the far left

简介

在本教程中,我们将开发一种方法来查找字符串中首次出现在最左边的重复字符。这意味着该字符首先出现在字符串的开头。为了确定第一个字符是否重复,我们遍历整个字符串并将每个字符与字符串的第一个字符进行匹配。为了解决这个任务,我们使用 C++ 编程语言的 find()、length() 和 end() 函数。

示例 1

String = “Tutorialspoint”
Copy after login
Output = The repeating character is “t”
Copy after login

在上面的示例中,输入字符串“tutorialspoint”最左边的字符是“t”,并且该字符在字符串中重复。

示例 2

String = “abcaabb”
Copy after login
Output = The repeating character is “a”
Copy after login

在上面的示例中,"a"是输入字符串的最左边的字符,并且在字符串的其他部分中也出现。因此,输出为"a"。

Example 3

的翻译为:

示例3

String = “programming”
Copy after login
Output = No repeating character.
Copy after login

在上面的示例中,输入字符串最左边的字符是“p”,并且它在字符串中的任何位置都不会重复。因此,输出是无重复字符。

  • find() − 它是一个字符串类函数,返回子字符串中重复值的索引值。

语法

find(char)
Copy after login
  • length() − 它是一个字符串类的函数,用于返回字符串的长度。

语法

string_name.length()
Copy after login
  • end() − 它是一个库函数,返回容器最后一个元素的迭代器值。

算法

  • 选择您选择的输入字符串。

  • 遍历整个字符串并将其字符保存在unordered_map中。

  • 将字符串的第一个字符与地图中保存的字符进行比较。

  • 检查是否有任何字符串字符与保存的字符匹配。

  • 打印输出。

示例

为了对上面列出的示例之一进行编码,我们在 C++ 中使用强力方法将每个字符与输入字符串中最左边的字符进行匹配。实现中使用的C++函数如下 -

#include 
#include 

using namespace std;

char findLeftmostRepeatedChar(string s){
   unordered_map 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;
}
Copy after login

输出

The leftmost repeated character in  << s <<  is 't'
Copy after login

结论

在本文中,我们开发了一种基于 C++ 的方法来查找输入字符串中最左边的重复字符。我们用一些例子来解释任务的含义。为了实现其中一个示例,我们使用了一些 C++ 库函数。我们将给定字符串的第一个字符与字符串的所有剩余字符进行比较。

The above is the detailed content of Repeating characters whose first occurrence is on the far left. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!