Heim > Backend-Entwicklung > C++ > Hauptteil

重复的字符,其第一次出现在最左边

PHPz
Freigeben: 2023-08-31 18:05:05
nach vorne
1009 人浏览过

重复的字符,其第一次出现在最左边

简介

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

示例 1

String = “Tutorialspoint”
Nach dem Login kopieren
Output = The repeating character is “t”
Nach dem Login kopieren

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

示例 2

String = “abcaabb”
Nach dem Login kopieren
Output = The repeating character is “a”
Nach dem Login kopieren

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

Example 3

的翻译为:

示例3

String = “programming”
Nach dem Login kopieren
Output = No repeating character.
Nach dem Login kopieren

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

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

语法

find(char)
Nach dem Login kopieren
  • length() − 它是一个字符串类的函数,用于返回字符串的长度。

语法

string_name.length()
Nach dem Login kopieren
  • 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;
}
Nach dem Login kopieren

输出

The leftmost repeated character in  << s <<  is 't'
Nach dem Login kopieren

结论

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

以上是重复的字符,其第一次出现在最左边的详细内容。更多信息请关注PHP中文网其他相关文章!

Verwandte Etiketten:
Quelle:tutorialspoint.com
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!