📜  在 O(1) 空间中查找字符串中的重复字符

📅  最后修改于: 2021-09-03 04:09:51             🧑  作者: Mango

给定一个字符串str ,任务是在不使用任何额外数据结构的情况下,按字典顺序查找给定字符串中存在的所有重复字符。

例子:

处理方法:按照以下步骤解决问题:

  • 初始化一个变量,比如first ,其中i首先检查字符(i + ‘a’) 是否至少出现在字符串中一次。
  • 初始化一个变量,表示第二,其中i第二检查的第i位,如果字符第(i +“A”)存在的字符串中的至少两倍或没有。
  • 遍历字符串的字符。对于每个i字符,检查str[i]是否已经出现在字符串。如果发现为真,则设置second 的(str[i] – ‘a’)位。
  • 否则,设置first 的(str[i] – ‘a’) th位。
  • 最后,迭代范围[0, 25]并检查一个第二个的第i位是否设置。如果发现为真,则打印(i + ‘a’)

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find duplicate characters
// in string without using any additional
// data structure
void findDuplicate(string str, int N)
{
 
    // Check if (i + 'a') is present
    // in str at least once or not.
    int first = 0;
 
    // Check if (i + 'a') is present
    // in str at least twice or not.
    int second = 0;
 
    // Iterate over the characters
    // of the string str
    for (int i = 0; i < N; i++) {
 
        // If str[i] has already occurred in str
        if (first & (1 << (str[i] - 'a'))) {
 
            // Set (str[i] - 'a')-th bit of second
            second
                = second | (1 << (str[i] - 'a'));
        }
        else {
 
            // Set (str[i] - 'a')-th bit of second
            first
                = first | (1 << (str[i] - 'a'));
        }
    }
 
    // Iterate over the range [0, 25]
    for (int i = 0; i < 26; i++) {
 
        // If i-th bit of both first
        // and second is Set
        if ((first & (1 << i))
            && (second & (1 << i))) {
 
            cout << char(i + 'a') << " ";
        }
    }
}
 
// Driver Code
int main()
{
    string str = "geeksforgeeks";
    int N = str.length();
 
    findDuplicate(str, N);
}


Java
// Java program for the above approach
public class GFG
{
 
  // Function to find duplicate characters
  // in string without using any additional
  // data structure
  static void findDuplicate(String str, int N)
  {
 
    // Check if (i + 'a') is present
    // in str at least once or not.
    int first = 0;
 
    // Check if (i + 'a') is present
    // in str at least twice or not.
    int second = 0;
 
    // Iterate over the characters
    // of the string str
    for (int i = 0; i < N; i++)
    {
 
      // If str[i] has already occurred in str
      if ((first & (1 << (str.charAt(i) - 'a'))) != 0)
      {
 
        // Set (str[i] - 'a')-th bit of second
        second
          = second | (1 << (str.charAt(i) - 'a'));
      }
      else
      {
 
        // Set (str[i] - 'a')-th bit of second
        first
          = first | (1 << (str.charAt(i) - 'a'));
      }
    }
 
    // Iterate over the range [0, 25]
    for (int i = 0; i < 26; i++)
    {
 
      // If i-th bit of both first
      // and second is Set
      if (((first & (1 << i))
           & (second & (1 << i))) != 0) {
 
        System.out.print((char)(i + 'a') + " ");
      }
    }
  }
 
  // Driver Code
  static public void main(String args[])
  {
    String str = "geeksforgeeks";
    int N = str.length();
 
    findDuplicate(str, N);
  }
}
 
// This code is contributed by AnkThon.


Python3
# Python 3 code added. program to implement
# the above approach
 
# Function to find duplicate characters
# in str1ing without using any additional
# data str1ucture
def findDuplicate(str1, N):
   
    # Check if (i + 'a') is present
    # in str1 at least once or not.
    first = 0
 
    # Check if (i + 'a') is present
    # in str1 at least twice or not.
    second = 0
 
    # Iterate over the characters
    # of the str1ing str1
    for i in range(N):
       
        # If str1[i] has already occurred in str1
        if (first & (1 << (ord(str1[i]) - 97))):
           
            # Set (str1[i] - 'a')-th bit of second
            second = second | (1 << (ord(str1[i]) - 97))
        else:
            # Set (str1[i] - 'a')-th bit of second
            first = first | (1 << (ord(str1[i]) - 97))
 
    # Iterate over the range [0, 25]
    for i in range(26):
       
        # If i-th bit of both first
        # and second is Set
        if ((first & (1 << i)) and (second & (1 << i))):
            print(chr(i + 97), end = " ")
 
# Driver Code
if __name__ == '__main__':
    str1 = "geeksforgeeks"
    N = len(str1)
    findDuplicate(str1, N)
 
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to find duplicate characters
  // in string without using any additional
  // data structure
  static void findDuplicate(string str, int N)
  {
 
    // Check if (i + 'a') is present
    // in str at least once or not.
    int first = 0;
 
    // Check if (i + 'a') is present
    // in str at least twice or not.
    int second = 0;
 
    // Iterate over the characters
    // of the string str
    for (int i = 0; i < N; i++) {
 
      // If str[i] has already occurred in str
      if ((first & (1 << (str[i] - 'a'))) != 0)
      {
 
        // Set (str[i] - 'a')-th bit of second
        second
          = second | (1 << (str[i] - 'a'));
      }
      else
      {
 
        // Set (str[i] - 'a')-th bit of second
        first
          = first | (1 << (str[i] - 'a'));
      }
    }
 
    // Iterate over the range [0, 25]
    for (int i = 0; i < 26; i++)
    {
 
      // If i-th bit of both first
      // and second is Set
      if (((first & (1 << i))
           & (second & (1 << i))) != 0) {
 
        Console.Write((char)(i + 'a') + " ");
      }
    }
  }
 
  // Driver Code
  static public void Main()
  {
    string str = "geeksforgeeks";
    int N = str.Length;
 
    findDuplicate(str, N);
  }
}
 
// This code is contributed by susmitakundugoaldanga.


输出:
e g k s

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live