给定一个由N 个字符组成的字符串S ,任务是按字典顺序打印所有长度为3 的回文字符串,这些字符串可以使用给定字符串S 的字符形成。
例子:
Input: S = “aabc”
Output:
aba
aca
Input: S = “ddadbac”
Output:
aba
aca
ada
dad
dbd
dcd
ddd
方法:给定的问题可以通过使用哈希的概念来解决,通过使用 Map 来实现它来存储字符。请按照以下步骤解决问题:
- 初始化一个 unordered_map,比如Hash ,它存储每个字符的计数。
- 将字符串的每个字符的频率存储在映射Hash 中。
- 初始化一组字符串,比如st ,以存储所有长度为3 的回文字符串。
- 使用变量i迭代字符[a, z] ,并执行以下步骤:
- 如果Hash[i] 的值为2 ,则使用变量j迭代范围 [a, z] 上的字符。如果Hash[j]的值大于0且i不等于j ,则将字符串(i + j + i)插入到 Set st 中。
- 否则,如果Hash[i]的值大于2则使用变量j迭代范围 [a, z] 上的字符,如果Hash[j]的值大于0 ,则插入字符串( i + j + i)在集合st 中。
- 完成上述步骤后,遍历集合st ,打印其中存储的字符串。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print all palindromic
// strings of length 3 that can be
// formed using characters of string S
void generatePalindrome(string S)
{
// Stores the count of character
unordered_map Hash;
// Traverse the string S
for (auto ch : S) {
Hash[ch]++;
}
// Stores all palindromic strings
set st;
// Iterate over the charchaters
// over the range ['a', 'z']
for (char i = 'a'; i <= 'z'; i++) {
// If Hash[ch] is equal to 2
if (Hash[i] == 2) {
// Iterate over the characters
// over the range ['a', 'z']
for (char j = 'a'; j <= 'z'; j++) {
// Stores all the
// palindromic string
string s = "";
if (Hash[j] && i != j) {
s += i;
s += j;
s += i;
// Push the s into
// the set st
st.insert(s);
}
}
}
// If Hash[i] is greater than
// or equal to 3
if (Hash[i] >= 3) {
// Iterate over charchaters
// over the range ['a', 'z']
for (char j = 'a';
j <= 'z'; j++) {
// Stores all the
// palindromic string
string s = "";
// If Hash[j] is positive
if (Hash[j]) {
s += i;
s += j;
s += i;
// Push s into
// the set st
st.insert(s);
}
}
}
}
// Iterate over the set
for (auto ans : st) {
cout << ans << "\n";
}
}
// Driver Code
int main()
{
string S = "ddabdac";
generatePalindrome(S);
return 0;
}
Python3
# Python3 program for the above approach
# Function to print all palindromic
# strings of length 3 that can be
# formed using characters of string S
def generatePalindrome(S):
# Stores the count of character
Hash = {}
# Traverse the string S
for ch in S:
Hash[ch] = Hash.get(ch,0) + 1
# Stores all palindromic strings
st = {}
# Iterate over the charchaters
# over the range ['a', 'z']
for i in range(ord('a'), ord('z') + 1):
# If Hash[ch] is equal to 2
if (chr(i) in Hash and Hash[chr(i)] == 2):
# Iterate over the characters
# over the range ['a', 'z']
for j in range(ord('a'), ord('z') + 1):
# Stores all the
# palindromic string
s = ""
if (chr(j) in Hash and i != j):
s += chr(i)
s += chr(j)
s += chr(i)
# Push the s into
# the set st
st[s] = 1
# If Hash[i] is greater than
# or equal to 3
if ((chr(i) in Hash) and Hash[chr(i)] >= 3):
# Iterate over charchaters
# over the range ['a', 'z']
for j in range(ord('a'), ord('z') + 1):
# Stores all the
# palindromic string
s = ""
# If Hash[j] is positive
if (chr(j) in Hash):
s += chr(i)
s += chr(j)
s += chr(i)
# Push s into
# the set st
st[s] = 1
# Iterate over the set
for ans in st:
print(ans)
# Driver Code
if __name__ == '__main__':
S = "ddabdac"
generatePalindrome(S)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to print all palindromic
// strings of length 3 that can be
// formed using characters of string S
static void generatePalindrome(string S)
{
// Stores the count of character
Dictionary Hash = new Dictionary();
// Traverse the string S
foreach (char ch in S)
{
if (Hash.ContainsKey(ch))
Hash[ch]++;
else
Hash.Add(ch, 1);
}
// Stores all palindromic strings
HashSet st = new HashSet();
// Iterate over the charchaters
// over the range ['a', 'z']
for(char i = 'a'; i <= 'z'; i++)
{
// If Hash[ch] is equal to 2
if (Hash.ContainsKey(i) && Hash[i] == 2)
{
// Iterate over the characters
// over the range ['a', 'z']
for(char j = 'a'; j <= 'z'; j++)
{
// Stores all the
// palindromic string
string s = "";
if (Hash.ContainsKey(j) && i != j)
{
s += i;
s += j;
s += i;
// Push the s into
// the set st
st.Add(s);
}
}
}
// If Hash[i] is greater than
// or equal to 3
if (Hash.ContainsKey(i) && Hash[i] >= 3)
{
// Iterate over charchaters
// over the range ['a', 'z']
for(char j = 'a'; j <= 'z'; j++)
{
// Stores all the
// palindromic string
string s = "";
// If Hash[j] is positive
if (Hash.ContainsKey(j))
{
s += i;
s += j;
s += i;
// Push s into
// the set st
st.Add(s);
}
}
}
}
// Iterate over the set
foreach(string ans in st)
{
Console.WriteLine(ans);
}
}
// Driver Code
public static void Main()
{
string S = "ddabdac";
generatePalindrome(S);
}
}
// This code is contributed by SURENDRA_GANGWAR
Javascript
输出:
aba
aca
ada
dad
dbd
dcd
ddd
时间复杂度: O(26*26)
辅助空间: O(26)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。