📌  相关文章
📜  在一次遍历中高效地找到字符串第一个重复的字符,而无需使用任何额外的数据结构

📅  最后修改于: 2021-10-27 07:25:47             🧑  作者: Mango

实现一种节省空间的算法来检查字符串的第一个重复字符,而无需在一次遍历中使用任何额外的数据结构。不允许使用额外的数据结构,如计数数组、散列等。
例子 :

Input :  abcfdeacf
Output : char = a, index= 6

这个想法是使用一个整数变量并使用其二进制表示中的位来存储字符是否存在。通常一个整数至少有 32 位,我们只需要存储 26 个字符的存在/不存在。

C++
// Efficiently check First repeated character
// in C++ program
#include
using namespace std;
 
// Returns -1 if all characters of str are
// unique.
// Assumptions : (1) str contains only characters
//                 from 'a' to 'z'
//             (2) integers are stored using 32
//                 bits
int FirstRepeated(string str)
{
    // An integer to store presence/absence
    // of 26 characters using its 32 bits.
    int checker = 0;
 
    for (int i = 0; i < str.length(); ++i)
    {
        int val = (str[i]-'a');
 
        // If bit corresponding to current
        // character is already set
        if ((checker & (1 << val)) > 0)
            return i;
 
        // set bit in checker
        checker |= (1 << val);
    }
 
    return -1;
}
 
// Driver code
int main()
{
    string s = "abcfdeacf";
    int i=FirstRepeated(s);
    if (i!=-1)
        cout <<"Char = "<< s[i] << "   and Index = "<


Java
// Efficiently check First repeated character
// in Java program
public class First_Repeated_char {
 
    static int FirstRepeated(String str)
    {
        // An integer to store presence/absence
        // of 26 characters using its 32 bits.
        int checker = 0;
      
        for (int i = 0; i < str.length(); ++i)
        {
            int val = (str.charAt(i)-'a');
      
            // If bit corresponding to current
            // character is already set
            if ((checker & (1 << val)) > 0)
                return i;
      
            // set bit in checker
            checker |= (1 << val);
        }
      
        return -1;
    }
      
    // Driver code
    public static void main(String args[])
    {
        String s = "abcfdeacf";
        int i=FirstRepeated(s);
        if (i!=-1)
           System.out.println("Char = "+ s.charAt(i) + "   and Index = "+i);
        else
            System.out.println( "No repeated Char");
    }
}
// This code is contributed by Sumit Ghosh


Python
# Efficiently check First repeated character
# in Python
 
# Returns -1 if all characters of str are
# unique.
# Assumptions : (1) str contains only characters
#                 from 'a' to 'z'
##             (2) integers are stored using 32
##                 bits
def FirstRepeated(string):
     
    # An integer to store presence/absence
    # of 26 characters using its 32 bits.
    checker = 0
  
    pos = 0
    for i in string:
        val = ord(i) - ord('a');
  
        # If bit corresponding to current
        # character is already set
        if ((checker & (1 << val)) > 0):
            return pos
  
        # set bit in checker
        checker |= (1 << val)
        pos += 1
  
    return -1
  
# Driver code
string = "abcfdeacf"
i = FirstRepeated(string)
if i != -1:
    print "Char = ", string[i], " and Index = ", i;
else:
    print "No repeated Char"
 
# This code is contributed by Sachin Bisht


C#
// C# program to Efficiently
// check First repeated character
using System;
 
public class First_Repeated_char {
 
    static int FirstRepeated(string str)
    {
        // An integer to store presence/absence
        // of 26 characters using its 32 bits.
        int checker = 0;
     
        for (int i = 0; i < str.Length; ++i)
        {
            int val = (str[i]-'a');
     
            // If bit corresponding to current
            // character is already set
            if ((checker & (1 << val)) > 0)
                return i;
     
            // set bit in checker
            checker |= (1 << val);
        }
     
        return -1;
    }
     
    // Driver code
    public static void Main()
    {
        string s = "abcfdeacf";
        int i=FirstRepeated(s);
        if (i!=-1)
           Console.WriteLine("Char = " + s[i] +
                          " and Index = " + i);
        else
            Console.WriteLine( "No repeated Char");
    }
}
 
// This code is contributed by vt_m.


PHP
 0)
            return $i;
 
        // set bit in checker
        $checker |= (1 << $val);
    }
 
    return -1;
}
 
// Driver code
$s = "abcfdeacf";
$i=FirstRepeated($s);
if ($i!=-1)
    echo "Char = " . $s[$i] .
         " and Index = " . $i;
else
    echo "No repeated Char";
 
// This code is contributed by ita_c
?>


Javascript


输出:

Char = a   and Index = 6

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程