c - 在 C 中重新排列字符串以形成回文
问题描述
给定 C 中的字符串。
如何重新排列给定的字符串以使其形成回文?
有没有办法在O(n)
时间复杂度和O(1)
空间复杂度上实现它?
解决方案
bool canFormPalindrome(string str)
{
// Create a count array and initialize all
// values as 0
int count[NO_OF_CHARS] = { 0 };
// For each character in input strings,
// increment count in the corresponding
// count array
for (int i = 0; str[i]; i++)
count[str[i]]++;
// Count odd occurring characters
int odd = 0;
for (int i = 0; i < NO_OF_CHARS; i++) {
if (count[i] & 1)
odd++;
if (odd > 1)
return false;
}
// Return true if odd count is 0 or 1,
return true;
}
推荐阅读
- sql - Oracle SQL 语言参考外部连接子句文档错误?
- flutter - 颤振手势检测器在堆栈小部件中不起作用
- r - 完全加入 dplyr
- java - Spring Boot 中用户定义的休息端点
- angular - 如何解决错误 GET http://localhost:8484/ 401
- python - 遍历熊猫中的特定列
- ssl - 我可以设置 443 或 8443 以外的 Tomcat SSL 端口吗
- cs50 - CS50 Pset4 棕褐色过滤器,错误在哪里?代码未通过 CS50 测试
- python - 在 Pandas 上创建具有变化窗口的滚动平均值
- scala - 循环遍历列表,每次获取越来越多的元素