当前位置:网站首页>Power button 1374. Generate each character string is an odd number
Power button 1374. Generate each character string is an odd number
2022-08-02 02:08:00 【Cold touch young fish】
题目
给你一个整数 n,请你返回一个含 n 个字符的字符串,其中每种字符在该字符串中都恰好出现 奇数次 .
返回的字符串必须只含小写英文字母.如果存在多个满足题目要求的字符串,则返回其中任意一个即可.
示例
输入:n = 4
输出:“pppz”
解释:“pppz” 是一个满足题目要求的字符串,因为 ‘p’ 出现 3 次,且 ‘z’ 出现 1 次.当然,还有很多其他字符串也满足题目要求,比如:“ohhh” 和 “love”.
输入:n = 2
输出:“xy”
解释:“xy” 是一个满足题目要求的字符串,因为 ‘x’ 和 ‘y’ 各出现 1 次.当然,还有很多其他字符串也满足题目要求,比如:“ag” 和 “ur”.
输入:n = 7
输出:“holasss”
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/generate-a-string-with-characters-that-have-odd-counts
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
方法1:贪心
Java实现
class Solution {
public String generateTheString(int n) {
StringBuffer res = new StringBuffer();
if (n % 2 == 0) {
for (int i = 0; i < n - 1; i++) res.append('a');
res.append('b');
return res.toString();
} else {
for (int i = 0; i < n; i++) res.append('a');
return res.toString();
}
}
}
边栏推荐
猜你喜欢
『网易实习』周记(一)
Redis Subscription and Redis Stream
Hash collisions and consistent hashing
【服务器数据恢复】服务器Raid5阵列mdisk磁盘离线的数据恢复案例
Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
ofstream,ifstream,fstream读写文件
YGG Guild Development Plan Season 1 Summary
【LeetCode每日一题】——654.最大二叉树
手写一个博客平台~第三天
Win Go development kit installation configuration, GoLand configuration
随机推荐
Ask God to answer, how should this kind of sql be written?
【服务器数据恢复】服务器Raid5阵列mdisk磁盘离线的数据恢复案例
Speed up your programs with bitwise operations
Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
Fly propeller power space future PIE - Engine Engine build earth science
『网易实习』周记(二)
LeetCode刷题日记: 33、搜索旋转排序数组
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
LeetCode brushing diary: 33. Search and rotate sorted array
Simple example of libcurl accessing url saved as file
typescript37-class的构造函数实例方法继承(extends)
The characteristics and principle of typescript29 - enumeration type
Record the pits where an error occurs when an array is converted to a collection, and try to use an array of packaging types for conversion
记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
垃圾回收器CMS和G1
typescript34-class的基本使用
2023年起,这些地区软考成绩低于45分也能拿证
typescript31-any类型
Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
3. Bean scope and life cycle