当前位置:网站首页>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;
}
边栏推荐
- [exercise 4-1] cake distribution
- Opencv learning log 32 edge extraction
- Find 3-friendly Integers
- Data storage in memory & loading into memory to make the program run
- [exercise-9] Zombie's Treasury test
- F - birthday cake (Shandong race)
- Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
- Alice and Bob (2021牛客暑期多校训练营1)
- 【练习-11】4 Values whose Sum is 0(和为0的4个值)
- frida hook so层、protobuf 数据解析
猜你喜欢
Write web games in C language
Nodejs+vue online fresh flower shop sales information system express+mysql
[exercise-5] (UVA 839) not so mobile (balance)
信息安全-威胁检测引擎-常见规则引擎底座性能比较
STM32 learning record: LED light flashes (register version)
信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
7-1 understand everything (20 points)
TCP's three handshakes and four waves
Determine the Photo Position
D - function (HDU - 6546) girls' competition
随机推荐
【练习-10】 Unread Messages(未读消息)
[exercise-6] (UVA 725) division = = violence
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
socket通讯
Ball Dropping
[exercise-6] (PTA) divide and conquer
Vs2019 initial use
China potato slicer market trend report, technical dynamic innovation and market forecast
Openwrt source code generation image
C basic grammar
X-Forwarded-For详解、如何获取到客户端IP
E. Breaking the Wall
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
Frida hook so layer, protobuf data analysis
If you want to apply for a programmer, your resume should be written like this [essence summary]
Determine the Photo Position
【练习-6】(Uva 725)Division(除法)== 暴力
[exercise-9] Zombie's Treasury test
Shell脚本编程
Information security - Analysis of security orchestration automation and response (soar) technology