Maison > développement back-end > C++ > Caractères répétitifs dont la première occurrence est à l'extrême gauche

Caractères répétitifs dont la première occurrence est à l'extrême gauche

PHPz
Libérer: 2023-08-31 18:05:05
avant
1242 Les gens l'ont consulté

Caractères répétitifs dont la première occurrence est à lextrême gauche

简介

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

示例 1

String = “Tutorialspoint”
Copier après la connexion
Output = The repeating character is “t”
Copier après la connexion

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

示例 2

String = “abcaabb”
Copier après la connexion
Output = The repeating character is “a”
Copier après la connexion

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

Example 3

的翻译为:

示例3

String = “programming”
Copier après la connexion
Output = No repeating character.
Copier après la connexion

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

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

语法

find(char)
Copier après la connexion
  • length() − 它是一个字符串类的函数,用于返回字符串的长度。

语法

string_name.length()
Copier après la connexion
  • end() − 它是一个库函数,返回容器最后一个元素的迭代器值。

算法

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

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

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

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

  • 打印输出。

示例

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

#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;
}
Copier après la connexion

输出

The leftmost repeated character in  << s <<  is 't'
Copier après la connexion

结论

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

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal