作为回文排列的最大偶数长度子串
给定一个字符串 ,任务是找到子串的最大长度可以排列成回文(即至少它的一个排列是回文)。请注意,子字符串的长度必须是偶数。
例子:
Input: str = “124565463”
Output: 6
“456546” is the valid sub-string
Input: str = “122313”
Output: 6
方法:使用两个变量:开始(包括)和结束(不包括)来跟踪给定字符串中正在考虑的当前子字符串的开始和结束索引。还使用一个名为count的字典,它记录一个字符在当前子字符串中出现的次数。现在,子字符串有两种可能的情况:
- 如果子串的长度是奇数,则不能在最终解中考虑。
- 如果子字符串的长度是偶数,那么只有当该子字符串中的每个字符出现偶数次时才可能是一个可能的解决方案,这可以使用字典count来完成。我们检查每个字符是否出现偶数次。如果是,那么我们将其作为可能的解决方案之一。然后我们通过在字符串中包含下一个字符来形成下一个子字符串,这可以通过简单地递增end来完成,并递归检查是否可以形成满足给定条件的更大长度的子字符串,并返回所有可能的最大值解决方案。
下面是上述方法的实现:
C++
// C++ code to find the maximum length of
// sub-string (of even length) which can be
// arranged into a Palindrome
#include
using namespace std;
unordered_map countt;
// function that returns true if the given
// sub-string can be arranged into a Palindrome
bool canBePalindrome(unordered_map &countt)
{
for (auto key : countt)
{
if (key.second & 1) return false;
}
return true;
}
// This function returns the maximum length of
// the sub-string (of even length) which can be
// arranged into a Palindrome
int maxPal(string str,
unordered_map &countt,
int start, int end)
{
// If we reach end of the string
if (end == str.length())
{
// if string is of even length
if ((end - start) % 2 == 0)
// if string can be arranged into a
// palindrome
if (canBePalindrome(countt)) return end - start;
return 0;
}
else
{
// Even length sub-string
if ((end - start) % 2 == 0)
{
// Check if current sub-string can be
// arranged into a palindrome
if (canBePalindrome(countt))
{
countt[str[end]]++;
return max(end - start, maxPal(str, countt,
start, end + 1));
}
else
{
countt[str[end]]++;
return maxPal(str, countt, start, end + 1);
}
}
// Odd length sub-string
else
{
countt[str[end]]++;
unordered_map c(countt.begin(),
countt.end());
int length = maxPal(str, c, start, end + 1);
countt[str[end]]--;
countt[str[start]]--;
return max(length, maxPal(str, countt,
start + 1, end));
}
}
}
// Driver code
int main(int argc, char const *argv[])
{
string str = "124565463";
int start = 0, end = 0;
cout << maxPal(str, countt, start, end) << endl;
return 0;
}
// This code is contributed by
// sanjeev2552
Java
// Java code to find the maximum length of
// sub-string (of even length) which can be
// arranged into a Palindrome
import java.io.*;
import java.util.*;
class GFG
{
static HashMap count = new HashMap<>();
// function that returns true if the given
// sub-string can be arranged into a Palindrome
static boolean canBePalindrome(HashMap count)
{
for (HashMap.Entry entry : count.entrySet())
if ((entry.getValue() & 1) == 1)
return false;
return true;
}
// This function returns the maximum length of
// the sub-string (of even length) which can be
// arranged into a Palindrome
static int maxPal(String str, int start, int end,
HashMap count)
{
// If we reach end of the string
if (end == str.length())
{
// if string is of even length
if ((end - start) % 2 == 0)
// if string can be arranged into a
// palindrome
if (canBePalindrome(count))
return end - start;
return 0;
}
else
{
// Even length sub-string
if ((end - start) % 2 == 0)
{
// Check if current sub-string can be
// arranged into a palindrome
if (canBePalindrome(count))
{
count.put((int) str.charAt(end),
count.get((int) str.charAt(end)) ==
null ? 1 : count.get((int) str.charAt(end)) + 1);
return Math.max(end - start,
maxPal(str, start, end + 1, count));
}
else
{
count.put((int) str.charAt(end),
count.get((int) str.charAt(end)) ==
null ? 1 : count.get((int) str.charAt(end)) + 1);
return maxPal(str, start, end + 1, count);
}
}
// Odd length sub-string
else
{
count.put((int) str.charAt(end),
count.get((int) str.charAt(end)) ==
null ? 1 : count.get((int) str.charAt(end)) + 1);
HashMap c = new HashMap<>(count);
int length = maxPal(str, start, end + 1, c);
count.put((int) str.charAt(end),
count.get((int) str.charAt(end)) ==
null ? -1 : count.get((int) str.charAt(end)) - 1);
count.put((int) str.charAt(start),
count.get((int) str.charAt(start)) ==
null ? -1 : count.get((int) str.charAt(start)) - 1);
return Math.max(length, maxPal(str,
start + 1, end, count));
}
}
}
// Driver Code
public static void main(String[] args)
{
String str = "124565463";
int start = 0, end = 0;
System.out.println(maxPal(str, start, end, count));
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python3 code to find the maximum length of sub-string
# (of even length) which can be arranged into a Palindrome
from collections import defaultdict
# function that returns true if the given
# sub-string can be arranged into a Palindrome
def canBePalindrome(count):
for key in count:
if count[key] % 2 != 0:
return False
return True
# This function returns the maximum length of
# the sub-string (of even length) which can be
# arranged into a Palindrome
def maxPal(string, count, start, end):
# If we reach end of the string
if end == len(string):
# if string is of even length
if (end-start) % 2 == 0:
# if string can be arranged into a
# palindrome
if canBePalindrome(count) == True:
return end-start
return 0
else:
# Even length sub-string
if (end-start) % 2 == 0:
# Check if current sub-string can be
# arranged into a palindrome
if canBePalindrome(count) == True:
count[string[end]] += 1
return max(end-start, maxPal(string, count, start, end + 1))
else:
count[string[end]] += 1
return maxPal(string, count, start, end + 1)
# Odd length sub-string
else:
count[string[end]] += 1
length = maxPal(string, count.copy(), start, end + 1)
count[string[end]] -= 1
count[string[start]] -= 1
return max(length, maxPal(string, count, start + 1, end))
# Driver code
string = '124565463'
start, end = 0, 0
count = defaultdict(lambda : 0)
print(maxPal(string, count, start, end))
输出:
6