📌  相关文章
📜  查找字符串中首先出现的重复字符

📅  最后修改于: 2022-05-13 01:57:07.725000             🧑  作者: Mango

查找字符串中首先出现的重复字符

给定一个字符串,找出字符串中第一个出现的重复字符。
(不是第一个重复的字符,在这里找到。)

例子:

Input  : geeksforgeeks
Output : g
(mind that it will be g, not e.)

被问到:高盛实习

使用 O(N^2) 复杂度的简单解决方案
解决方案是遍历每个字符的字符串并在字符串的其余部分中搜索相同的字符串。这将需要两个循环,因此不是最佳的。

C++
// C++ program to find the first
// character that is repeated
#include 
#include 
 
using namespace std;
int findRepeatFirstN2(char* s)
{
    // this is O(N^2) method
    int p = -1, i, j;
    for (i = 0; i < strlen(s); i++)
    {
        for (j = i + 1; j < strlen(s); j++)
        {
            if (s[i] == s[j])
            {
                p = i;
                break;
            }
        }
        if (p != -1)
            break;
    }
 
    return p;
}
 
// Driver code
int main()
{
    char str[] = "geeksforgeeks";
    int pos = findRepeatFirstN2(str);
    if (pos == -1)
        cout << "Not found";
    else
        cout << str[pos];
    return 0;
}
 
// This code is contributed
// by Akanksha Rai


C
// C program to find the first character that
// is repeated
#include 
#include 
 
int findRepeatFirstN2(char* s)
{
    // this is O(N^2) method
    int p = -1, i, j;
    for (i = 0; i < strlen(s); i++) {
        for (j = i + 1; j < strlen(s); j++) {
            if (s[i] == s[j]) {
                p = i;
                break;
            }
        }
        if (p != -1)
            break;
    }
 
    return p;
}
 
// Driver code
int main()
{
    char str[] = "geeksforgeeks";
    int pos = findRepeatFirstN2(str);
    if (pos == -1)
        printf("Not found");
    else
        printf("%c", str[pos]);
    return 0;
}


Java
// Java program to find the first character
// that is repeated
import java.io.*;
import java.util.*;
 
class GFG {
 
    static int findRepeatFirstN2(String s)
    {
         
        // this is O(N^2) method
        int p = -1, i, j;
        for (i = 0; i < s.length(); i++)
        {
            for (j = i + 1; j < s.length(); j++)
            {
                if (s.charAt(i) == s.charAt(j))
                {
                    p = i;
                    break;
                }
            }
            if (p != -1)
                break;
        }
     
        return p;
    }
     
    // Driver code
    static public void main (String[] args)
    {
        String str = "geeksforgeeks";
        int pos = findRepeatFirstN2(str);
         
        if (pos == -1)
            System.out.println("Not found");
        else
        System.out.println( str.charAt(pos));
    }
}
 
// This code is contributed by anuj_67.


Python3
# Python3 program to find the first
# character that is repeated
 
def findRepeatFirstN2(s):
 
    # this is O(N^2) method
    p = -1
    for i in range(len(s)):
     
        for j in range (i + 1, len(s)):
         
            if (s[i] == s[j]):
                p = i
                break
             
        if (p != -1):
            break
 
    return p
 
# Driver code
if __name__ == "__main__":
 
    str = "geeksforgeeks"
    pos = findRepeatFirstN2(str)
    if (pos == -1):
        print ("Not found")
    else:
        print (str[pos])
     
# This code is contributed
# by ChitraNayal


C#
// C# program to find the first character
// that is repeated
using System;
 
class GFG {
 
    static int findRepeatFirstN2(string s)
    {
         
        // this is O(N^2) method
        int p = -1, i, j;
        for (i = 0; i < s.Length; i++)
        {
            for (j = i + 1; j < s.Length; j++)
            {
                if (s[i] == s[j])
                {
                    p = i;
                    break;
                }
            }
            if (p != -1)
                break;
        }
     
        return p;
    }
     
    // Driver code
    static public void Main ()
    {
        string str = "geeksforgeeks";
        int pos = findRepeatFirstN2(str);
         
        if (pos == -1)
            Console.WriteLine("Not found");
        else
        Console.WriteLine( str[pos]);
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


C++
// C++ program to find the first character that
// is repeated
#include
 
using namespace std;
// 256 is taken just to ensure nothing is left,
// actual max ASCII limit is 128
#define MAX_CHAR 256
 
int findRepeatFirst(char* s)
{
    // this is optimized method
    int p = -1, i, k;
 
    // initialized counts of occurrences of
    // elements as zero
    int hash[MAX_CHAR] = { 0 };
 
    // initialized positions
    int pos[MAX_CHAR];
 
    for (i = 0; i < strlen(s); i++) {
        k = (int)s[i];
        if (hash[k] == 0) {
            hash[k]++;
            pos[k] = i;
        } else if (hash[k] == 1)
            hash[k]++;
    }
 
    for (i = 0; i < MAX_CHAR; i++) {
        if (hash[i] == 2) {
            if (p == -1) // base case
                p = pos[i];
            else if (p > pos[i])
                p = pos[i];
        }
    }
 
    return p;
}
 
// Driver code
int main()
{
    char str[] = "geeksforgeeks";
    int pos = findRepeatFirst(str);
    if (pos == -1)
        cout << "Not found";
    else
        cout << str[pos];
    return 0;
}
 
// This code is contributed
// by Akanksha Rai


C
// C program to find the first character that
// is repeated
#include 
#include 
 
// 256 is taken just to ensure nothing is left,
// actual max ASCII limit is 128
#define MAX_CHAR 256
 
int findRepeatFirst(char* s)
{
    // this is optimized method
    int p = -1, i, k;
 
    // initialized counts of occurrences of
    // elements as zero
    int hash[MAX_CHAR] = { 0 };
 
    // initialized positions
    int pos[MAX_CHAR];
 
    for (i = 0; i < strlen(s); i++) {
        k = (int)s[i];
        if (hash[k] == 0) {
            hash[k]++;
            pos[k] = i;
        } else if (hash[k] == 1)
            hash[k]++;
    }
 
    for (i = 0; i < MAX_CHAR; i++) {
        if (hash[i] == 2) {
            if (p == -1) // base case
                p = pos[i];
            else if (p > pos[i])
                p = pos[i];
        }
    }
 
    return p;
}
 
// Driver code
int main()
{
    char str[] = "geeksforgeeks";
    int pos = findRepeatFirst(str);
    if (pos == -1)
        printf("Not found");
    else
        printf("%c", str[pos]);
    return 0;
}


Java
// Java Program to find the first character 
// that is repeated
 
import java.util.*;
import java.lang.*;
 
public class GFG
{
    public static int findRepeatFirst(String s)
    {
        // this is optimized method
        int p = -1, i, k;
 
        // initialized counts of occurrences of
        // elements as zero
        int MAX_CHAR = 256;
        int hash[] = new int[MAX_CHAR];
 
        // initialized positions
        int pos[] = new int[MAX_CHAR];
 
        for (i = 0; i < s.length(); i++)
        {
            k = (int)s.charAt(i);
            if (hash[k] == 0)
            {
                hash[k]++;
                pos[k] = i;
            }
            else if (hash[k] == 1)
                hash[k]++;
        }
 
        for (i = 0; i < MAX_CHAR; i++)
        {
            if (hash[i] == 2)
            {
                if (p == -1) // base case
                    p = pos[i];
                else if (p > pos[i])
                    p = pos[i];
            }
        }
 
        return p;
    }
 
// Driver code
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
        int pos = findRepeatFirst(str);
        if (pos == -1)
            System.out.println("Not found");
        else
            System.out.println(str.charAt(pos));
    }
}
 
// Code Contributed by Mohit Gupta_OMG


Python3
# Python 3 program to find the first
# character that is repeated
 
# 256 is taken just to ensure nothing
# is left, actual max ASCII limit is 128
 
MAX_CHAR = 256
 
def findRepeatFirst(s):
     
    # this is optimized method
    p = -1
 
    # initialized counts of occurrences
    # of elements as zero
    hash = [0 for i in range(MAX_CHAR)]
 
    # initialized positions
    pos = [0 for i in range(MAX_CHAR)]
 
    for i in range(len(s)):
        k = ord(s[i])
        if (hash[k] == 0):
            hash[k] += 1
            pos[k] = i
        elif(hash[k] == 1):
            hash[k] += 1
 
    for i in range(MAX_CHAR):
        if (hash[i] == 2):
            if (p == -1): # base case
                p = pos[i]
            elif(p > pos[i]):
                p = pos[i]
    return p
 
# Driver code
if __name__ == '__main__':
    str = "geeksforgeeks"
    pos = findRepeatFirst(str);
    if (pos == -1):
        print("Not found")
    else:
        print(str[pos])
         
# This code is contributed by
# Shashank_Sharma


C#
// C# Program to find the first character 
// that is repeated
using System;
public class GFG
{
    public static int findRepeatFirst(string s)
    {
        // this is optimized method
        int p = -1, i, k;
  
        // initialized counts of occurrences of
        // elements as zero
        int MAX_CHAR = 256;
        int []hash = new int[MAX_CHAR];
  
        // initialized positions
        int []pos = new int[MAX_CHAR];
  
        for (i = 0; i < s.Length; i++)
        {
            k = (int)s[i];
            if (hash[k] == 0)
            {
                hash[k]++;
                pos[k] = i;
            }
            else if (hash[k] == 1)
                hash[k]++;
        }
  
        for (i = 0; i < MAX_CHAR; i++)
        {
            if (hash[i] == 2)
            {
                if (p == -1) // base case
                    p = pos[i];
                else if (p > pos[i])
                    p = pos[i];
            }
        }
  
        return p;
    }
  
    // Driver code
    public static void Main()
    {
        string str = "geeksforgeeks";
        int pos = findRepeatFirst(str);
        if (pos == -1)
            Console.Write("Not found");
        else
            Console.Write(str[pos]);
    }
}
  
// This code is contributed by nitin mittal.


Javascript


Python3
# Python implementation
from collections import Counter
 
# Function which repeats
# first repeating character
def printrepeated(string):
   
    # Calculating frequencies
    # using Counter function
    freq = Counter(string)
     
    # Traverse the string
    for i in string:
        if(freq[i] > 1):
            print(i)
            break
 
 
# Driver code
string = "geeksforgeeks"
 
# passing string to printrepeated function
printrepeated(string)
 
# this code is contributed by vikkycirus


C++
#include 
#include
#define INT_MAX 2147483647
using namespace std;
 
// Function to find left most repeating character.
char firstRep (string s)
    {
        unordered_map map;
        char c='#';
        int index=INT_MAX;
         
        // single traversal of string.
        for(int i=0;i


Javascript


输出
g

通过计算出现次数进行优化
该解决方案通过使用以下技术进行了优化:
1. 我们遍历字符串并使用 ASCII 码对字符进行散列。如果找到则存储 1,如果再次找到则存储 2。另外,存储第一个找到的字母的位置。
2. 我们在哈希数组上运行一个循环,现在我们找到任何重复字符的最小位置。

这将具有O(N)的运行时间。

C++

// C++ program to find the first character that
// is repeated
#include
 
using namespace std;
// 256 is taken just to ensure nothing is left,
// actual max ASCII limit is 128
#define MAX_CHAR 256
 
int findRepeatFirst(char* s)
{
    // this is optimized method
    int p = -1, i, k;
 
    // initialized counts of occurrences of
    // elements as zero
    int hash[MAX_CHAR] = { 0 };
 
    // initialized positions
    int pos[MAX_CHAR];
 
    for (i = 0; i < strlen(s); i++) {
        k = (int)s[i];
        if (hash[k] == 0) {
            hash[k]++;
            pos[k] = i;
        } else if (hash[k] == 1)
            hash[k]++;
    }
 
    for (i = 0; i < MAX_CHAR; i++) {
        if (hash[i] == 2) {
            if (p == -1) // base case
                p = pos[i];
            else if (p > pos[i])
                p = pos[i];
        }
    }
 
    return p;
}
 
// Driver code
int main()
{
    char str[] = "geeksforgeeks";
    int pos = findRepeatFirst(str);
    if (pos == -1)
        cout << "Not found";
    else
        cout << str[pos];
    return 0;
}
 
// This code is contributed
// by Akanksha Rai

C

// C program to find the first character that
// is repeated
#include 
#include 
 
// 256 is taken just to ensure nothing is left,
// actual max ASCII limit is 128
#define MAX_CHAR 256
 
int findRepeatFirst(char* s)
{
    // this is optimized method
    int p = -1, i, k;
 
    // initialized counts of occurrences of
    // elements as zero
    int hash[MAX_CHAR] = { 0 };
 
    // initialized positions
    int pos[MAX_CHAR];
 
    for (i = 0; i < strlen(s); i++) {
        k = (int)s[i];
        if (hash[k] == 0) {
            hash[k]++;
            pos[k] = i;
        } else if (hash[k] == 1)
            hash[k]++;
    }
 
    for (i = 0; i < MAX_CHAR; i++) {
        if (hash[i] == 2) {
            if (p == -1) // base case
                p = pos[i];
            else if (p > pos[i])
                p = pos[i];
        }
    }
 
    return p;
}
 
// Driver code
int main()
{
    char str[] = "geeksforgeeks";
    int pos = findRepeatFirst(str);
    if (pos == -1)
        printf("Not found");
    else
        printf("%c", str[pos]);
    return 0;
}

Java

// Java Program to find the first character 
// that is repeated
 
import java.util.*;
import java.lang.*;
 
public class GFG
{
    public static int findRepeatFirst(String s)
    {
        // this is optimized method
        int p = -1, i, k;
 
        // initialized counts of occurrences of
        // elements as zero
        int MAX_CHAR = 256;
        int hash[] = new int[MAX_CHAR];
 
        // initialized positions
        int pos[] = new int[MAX_CHAR];
 
        for (i = 0; i < s.length(); i++)
        {
            k = (int)s.charAt(i);
            if (hash[k] == 0)
            {
                hash[k]++;
                pos[k] = i;
            }
            else if (hash[k] == 1)
                hash[k]++;
        }
 
        for (i = 0; i < MAX_CHAR; i++)
        {
            if (hash[i] == 2)
            {
                if (p == -1) // base case
                    p = pos[i];
                else if (p > pos[i])
                    p = pos[i];
            }
        }
 
        return p;
    }
 
// Driver code
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
        int pos = findRepeatFirst(str);
        if (pos == -1)
            System.out.println("Not found");
        else
            System.out.println(str.charAt(pos));
    }
}
 
// Code Contributed by Mohit Gupta_OMG

Python3

# Python 3 program to find the first
# character that is repeated
 
# 256 is taken just to ensure nothing
# is left, actual max ASCII limit is 128
 
MAX_CHAR = 256
 
def findRepeatFirst(s):
     
    # this is optimized method
    p = -1
 
    # initialized counts of occurrences
    # of elements as zero
    hash = [0 for i in range(MAX_CHAR)]
 
    # initialized positions
    pos = [0 for i in range(MAX_CHAR)]
 
    for i in range(len(s)):
        k = ord(s[i])
        if (hash[k] == 0):
            hash[k] += 1
            pos[k] = i
        elif(hash[k] == 1):
            hash[k] += 1
 
    for i in range(MAX_CHAR):
        if (hash[i] == 2):
            if (p == -1): # base case
                p = pos[i]
            elif(p > pos[i]):
                p = pos[i]
    return p
 
# Driver code
if __name__ == '__main__':
    str = "geeksforgeeks"
    pos = findRepeatFirst(str);
    if (pos == -1):
        print("Not found")
    else:
        print(str[pos])
         
# This code is contributed by
# Shashank_Sharma

C#

// C# Program to find the first character 
// that is repeated
using System;
public class GFG
{
    public static int findRepeatFirst(string s)
    {
        // this is optimized method
        int p = -1, i, k;
  
        // initialized counts of occurrences of
        // elements as zero
        int MAX_CHAR = 256;
        int []hash = new int[MAX_CHAR];
  
        // initialized positions
        int []pos = new int[MAX_CHAR];
  
        for (i = 0; i < s.Length; i++)
        {
            k = (int)s[i];
            if (hash[k] == 0)
            {
                hash[k]++;
                pos[k] = i;
            }
            else if (hash[k] == 1)
                hash[k]++;
        }
  
        for (i = 0; i < MAX_CHAR; i++)
        {
            if (hash[i] == 2)
            {
                if (p == -1) // base case
                    p = pos[i];
                else if (p > pos[i])
                    p = pos[i];
            }
        }
  
        return p;
    }
  
    // Driver code
    public static void Main()
    {
        string str = "geeksforgeeks";
        int pos = findRepeatFirst(str);
        if (pos == -1)
            Console.Write("Not found");
        else
            Console.Write(str[pos]);
    }
}
  
// This code is contributed by nitin mittal.

Javascript


输出
g

方法#3:使用内置Python函数:

方法:

  • 使用 Counter()函数计算所有字符的所有频率。
  • 遍历字符串并检查是否有任何元素的频率大于 1。
  • 打印字符并打破循环

下面是实现:

Python3

# Python implementation
from collections import Counter
 
# Function which repeats
# first repeating character
def printrepeated(string):
   
    # Calculating frequencies
    # using Counter function
    freq = Counter(string)
     
    # Traverse the string
    for i in string:
        if(freq[i] > 1):
            print(i)
            break
 
 
# Driver code
string = "geeksforgeeks"
 
# passing string to printrepeated function
printrepeated(string)
 
# this code is contributed by vikkycirus
输出
g

时间复杂度:O(n)

空间复杂度:O(n)

方法#4:仅通过给定字符串的单次遍历来解决。

算法 :

1.从左到右遍历字符串。

2.如果当前字符不存在于哈希图中,则将该字符连同其索引一起推送。

3.如果当前字符已经存在于hash map中,则获取当前字符的索引(从hash map中)并与之前找到的重复字符的索引进行比较。

4 .如果当前索引较小,则更新索引。

C++

#include 
#include
#define INT_MAX 2147483647
using namespace std;
 
// Function to find left most repeating character.
char firstRep (string s)
    {
        unordered_map map;
        char c='#';
        int index=INT_MAX;
         
        // single traversal of string.
        for(int i=0;i

Javascript


输出
b

时间复杂度: O(N)

空间复杂度: O(N)