当前位置:网站首页>409. Longest palindrome
409. Longest palindrome
2022-07-06 16:08:00 【mrbone9】
Address :
Power button https://leetcode-cn.com/problems/longest-palindrome/
subject :
Given a string of uppercase and lowercase letters s , return Constructed from these letters The longest palindrome string .
In the process of construction , Please note that Case sensitive . such as "Aa" Can't be treated as a palindrome string .
Example 1:
Input :s = "abccccdd" Output :7 explain : The longest palindrome string we can construct is "dccaccd", Its length is 7. |
Example 2:
Input :s = "a" Input :1 |
Example 3:
Input :s = "bb" Input : 2 |
Tips :
1 <= s.length <= 2000 s Only lowercase and / Or capital letters |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/longest-palindrome
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
Palindromes mean pairs , Even number , As for whether there is an odd number in the middle , All palindromes
So we use the idea of counting arrays to complete
Count array can refer to :
Counting sorting of sorting algorithm
Method 1 、 Count array
int longestPalindrome(char * s){
int count[58]={0};
int cnt = 0;
int odd = 0;
int i = 0;
while(s[i])
{
int idx = s[i] - 'A';
//printf("s[%d]=%d, idx=%d\n", i, s[i], idx);
count[idx]++;
i++;
}
for(i=0; i<58; i++)
{
if(count[i] != 0)
{
if(count[i] & 1)
odd = 1;
cnt += count[i] / 2;
}
}
cnt *= 2;
if(odd == 1)
cnt++;
return cnt;
}
边栏推荐
- 初入Redis
- Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
- 渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
- 1903. Maximum odd number in string
- PySide6 信号、槽
- Perform general operations on iptables
- Penetration test (4) -- detailed explanation of meterpreter command
- MySQL grants the user the operation permission of the specified content
- 快速转 TypeScript 指南
- Programmers, what are your skills in code writing?
猜你喜欢
PySide6 信号、槽
Data storage in memory & loading into memory to make the program run
渗透测试 ( 4 ) --- Meterpreter 命令详解
Information security - threat detection - detailed design of NAT log access threat detection platform
Penetration test (1) -- necessary tools, navigation
Gartner:关于零信任网络访问最佳实践的五个建议
Information security - Analysis of security orchestration automation and response (soar) technology
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
Determine the Photo Position
Pyside6 signal, slot
随机推荐
【练习-6】(PTA)分而治之
Opencv learning log 13 corrosion, expansion, opening and closing operations
初入Redis
MySQL授予用户指定内容的操作权限
Borg maze (bfs+ minimum spanning tree) (problem solving report)
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
信息安全-安全编排自动化与响应 (SOAR) 技术解析
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
Interval sum ----- discretization
Determine the Photo Position
Record of brushing questions with force deduction -- complete knapsack problem (I)
Openwrt source code generation image
Borg Maze (BFS+最小生成树)(解题报告)
What is the difficulty of programming?
B - Code Party (girls' competition)
Penetration test (3) -- Metasploit framework (MSF)
Opencv learning log 12 binarization of Otsu method
Socket communication
Nodejs+vue网上鲜花店销售信息系统express+mysql
E. Breaking the Wall