📌  相关文章
📜  有效地检查字符串是否具有所有唯一字符,而无需使用任何其他数据结构

📅  最后修改于: 2021-04-23 07:50:32             🧑  作者: Mango

实现一个节省空间的算法来确定一个字符串(从“A”到“Z”的字符)拥有所有独特的字符或没有。不允许使用其他数据结构,例如count数组,hash等。
预期时间复杂度:O(n)
例子 :

Input  : str = "aaabbccdaa"
Output : No

Input  : str = "abcd"
Output : Yes

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

C++
// A space efficient C++ program to check if
// all characters of string are unique.
#include
using namespace std;
 
// Returns true if all characters of str are
// unique.
// Assumptions : (1) str contains only characters
//                   from 'a' to 'z'
//               (2) integers are stored using 32
//                   bits
bool areChractersUnique(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 false;
 
        // set bit in checker
        checker |=  (1 << val);
    }
 
    return true;
}
 
// Driver code
int main()
{
    string s = "aaabbccdaa";
    if (areChractersUnique(s))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// A space efficient Java program to check if
// all characters of string are unique.
class GFG {
         
    // Returns true if all characters of str are
    // unique.
    // Assumptions : (1) str contains only characters
    //                 from 'a' to 'z'
    //             (2) integers are stored using 32
    //                 bits
    static boolean areChractersUnique(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 false;
     
            // set bit in checker
            checker |= (1 << val);
        }
     
        return true;
    }
     
    //driver code
    public static void main (String[] args)
    {
        String s = "aaabbccdaa";
         
        if (areChractersUnique(s))
            System.out.print("Yes");
        else
            System.out.print("No");
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# A space efficeint c++ program to check if
# all characters of string are unique
 
# Returns true 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 areCharactersUnique(s):
     
    # An integer to store presence/absence
    # of 26 characters using its 32 bits
    checker = 0
     
    for i in range(len(s)):
         
        val = ord(s[i]) - ord('a')
         
        # If bit corresponding to current
        # character is already set
        if (checker & (1 << val)) > 0:
            return False
         
        # set bit in checker
        checker |= (1 << val)
         
    return True
     
# Driver code
s = "aaabbccdaa"
if areCharactersUnique(s):
    print("Yes")
else:
    print("No")
     
# This code is contributed
# by Mohit Kumar


C#
// A space efficient program
// to check if all characters
// of string are unique.
using System;
 
class GFG {
 
    // Returns true if all characters
    // of str are unique. Assumptions:
    // (1)str contains only characters
    // from 'a' to 'z'.(2)integers are
    // stored using 32 bits
    static bool areChractersUnique(string str)
    {
        // An integer to store presence
        // or 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 false;
 
            // set bit in checker
            checker |= (1 << val);
        }
 
        return true;
    }
 
    // Driver code
    public static void Main()
    {
        string s = "aaabbccdaa";
 
        if (areChractersUnique(s))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
 
// This code is contributed by Anant Agarwal.


PHP
 0)
            return false;
 
        // set bit in checker
        $checker |= (1 << $val);
    }
 
    return true;
}
 
// Driver code
$s = "aaabbccdaa";
if (areChractersUnique($s))
    echo "Yes";
else
    echo "No";
 
// This code is contributed by aj_36
?>


Javascript


输出 :

No