按排序顺序查找并打印两个给定字符串的不常见字符。这里不常见的字符意味着该字符要么出现在一个字符串,要么出现在另一个字符串但不同时出现在这两个字符串中。字符串仅包含小写字符并且可以包含重复项。
资料来源:亚马逊面试体验 | 355套(1年经验)
例子:
Input: str1 = “characters”, str2 = “alphabets”
Output: b c l p r
Input: str1 = “geeksforgeeks”, str2 = “geeksquiz”
Output: f i o q r u z
朴素的方法:使用两个循环,对于第一个字符串的每个字符检查它是否存在于第二个字符串。同样,对于第二个字符串的每个字符,检查它是否存在于第一个字符串。
时间复杂度:需要 O(n^2) 和额外的来处理重复项。
有效的方法:一种有效的方法是使用散列。
- 对所有小写字符使用大小为 26 的哈希表。
- 最初,将每个字符的存在标记为“0”(表示该字符不存在于两个字符串)。
- 遍历第一个字符串,并将哈希表中第一个字符串的每个字符的存在标记为“1”(表示第一个字符串)。
- 现在,遍历第二个字符串。对于 2nd 字符串 的每个字符,检查它在哈希表中的存在是否为“1”。如果它是“1”,则将其存在标记为“-1”(表示该字符对两个字符串是共同的),否则将其存在标记为“2”(表示第二个字符串)。
下图是上述方法的试运行:
下面是上述方法的实现:
C++
// C++ implementation to find the uncommon
// characters of the two strings
#include
using namespace std;
// size of the hash table
const int MAX_CHAR = 26;
// function to find the uncommon characters
// of the two strings
void findAndPrintUncommonChars(string str1, string str2)
{
// mark presence of each character as 0
// in the hash table 'present[]'
int present[MAX_CHAR];
for (int i=0; i
Java
// Java implementation to find the uncommon
// characters of the two strings
class GFG
{
// size of the hash table
static int MAX_CHAR = 26;
// function to find the uncommon
// characters of the two strings
static void findAndPrintUncommonChars(String str1,
String str2)
{
// mark presence of each character as 0
// in the hash table 'present[]'
int present[] = new int[MAX_CHAR];
for (int i = 0; i < MAX_CHAR; i++)
{
present[i] = 0;
}
int l1 = str1.length();
int l2 = str2.length();
// for each character of str1, mark its
// presence as 1 in 'present[]'
for (int i = 0; i < l1; i++)
{
present[str1.charAt(i) - 'a'] = 1;
}
// for each character of str2
for (int i = 0; i < l2; i++)
{
// if a character of str2 is also present
// in str1, then mark its presence as -1
if (present[str2.charAt(i) - 'a'] == 1
|| present[str2.charAt(i) - 'a'] == -1)
{
present[str2.charAt(i) - 'a'] = -1;
}
// else mark its presence as 2
else
{
present[str2.charAt(i) - 'a'] = 2;
}
}
// print all the uncommon characters
for (int i = 0; i < MAX_CHAR; i++)
{
if (present[i] == 1 || present[i] == 2)
{
System.out.print((char) (i + 'a') + " ");
}
}
}
// Driver code
public static void main(String[] args)
{
String str1 = "characters";
String str2 = "alphabets";
findAndPrintUncommonChars(str1, str2);
}
}
// This code is contributed by Rajput-JI
Python 3
# Python 3 implementation to find the
# uncommon characters of the two strings
# size of the hash table
MAX_CHAR = 26
# function to find the uncommon characters
# of the two strings
def findAndPrintUncommonChars(str1, str2):
# mark presence of each character as 0
# in the hash table 'present[]'
present = [0] * MAX_CHAR
for i in range(0, MAX_CHAR):
present[i] = 0
l1 = len(str1)
l2 = len(str2)
# for each character of str1, mark its
# presence as 1 in 'present[]'
for i in range(0, l1):
present[ord(str1[i]) - ord('a')] = 1
# for each character of str2
for i in range(0, l2):
# if a character of str2 is also present
# in str1, then mark its presence as -1
if(present[ord(str2[i]) - ord('a')] == 1 or
present[ord(str2[i]) - ord('a')] == -1):
present[ord(str2[i]) - ord('a')] = -1
# else mark its presence as 2
else:
present[ord(str2[i]) - ord('a')] = 2
# print all the uncommon characters
for i in range(0, MAX_CHAR):
if(present[i] == 1 or present[i] == 2):
print(chr(i + ord('a')), end = " ")
# Driver Code
if __name__ == "__main__":
str1 = "characters"
str2 = "alphabets"
findAndPrintUncommonChars(str1, str2)
# This code is contributed
# by Sairahul099
C#
// C# implementation to find the uncommon
// characters of the two strings
using System;
class GFG
{
// size of the hash table
static int MAX_CHAR = 26;
// function to find the uncommon
// characters of the two strings
static void findAndPrintUncommonChars(String str1,
String str2)
{
// mark presence of each character as 0
// in the hash table 'present[]'
int []present = new int[MAX_CHAR];
for (int i = 0; i < MAX_CHAR; i++)
{
present[i] = 0;
}
int l1 = str1.Length;
int l2 = str2.Length;
// for each character of str1, mark its
// presence as 1 in 'present[]'
for (int i = 0; i < l1; i++)
{
present[str1[i] - 'a'] = 1;
}
// for each character of str2
for (int i = 0; i < l2; i++)
{
// if a character of str2 is also present
// in str1, then mark its presence as -1
if (present[str2[i] - 'a'] == 1
|| present[str2[i] - 'a'] == -1)
{
present[str2[i] - 'a'] = -1;
}
// else mark its presence as 2
else
{
present[str2[i] - 'a'] = 2;
}
}
// print all the uncommon characters
for (int i = 0; i < MAX_CHAR; i++)
{
if (present[i] == 1 || present[i] == 2)
{
Console.Write((char) (i + 'a') + " ");
}
}
}
// Driver code
public static void Main(String[] args)
{
String str1 = "characters";
String str2 = "alphabets";
findAndPrintUncommonChars(str1, str2);
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
b c l p r
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。