当前位置:网站首页>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();
}
}
}
边栏推荐
- Fly propeller power space future PIE - Engine Engine build earth science
- typescript36-class的构造函数实例方法
- Redis Subscription and Redis Stream
- 待读书单列表
- Redis 订阅与 Redis Stream
- CodeTon Round 2 D. Magical Array
- libcurl访问url保存为文件的简单示例
- LeetCode刷题日记: 33、搜索旋转排序数组
- openGauss切换后state状态显示不对
- typescript37-class的构造函数实例方法继承(extends)
猜你喜欢
Fly propeller power space future PIE - Engine Engine build earth science
电子制造仓储条码管理系统解决方案
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
力扣 1161. 最大层内元素和
The characteristics and principle of typescript29 - enumeration type
typescript33-typescript高级概述
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
手写博客平台~第二天
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021: Interpretation
Constructor instance method inheritance of typescript37-class (extends)
随机推荐
检查IP或端口是否被封
成都openGauss用户组招募啦!
哈希冲突和一致性哈希
swift项目,sqlcipher3 -&gt; 4,无法打开旧版数据库有办法解决吗
Fly propeller power space future PIE - Engine Engine build earth science
LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
LeetCode Review Diary: 153. Find the Minimum Value in a Rotated Sort Array
电子制造仓储条码管理系统解决方案
个人博客系统项目测试
乱七八糟的网站
3. Bean scope and life cycle
LeetCode刷题日记:74. 搜索二维矩阵
Day116. Shangyitong: Details of appointment registration ※
Hash collisions and consistent hashing
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
秒懂大模型 | 3步搞定AI写摘要
Redis 持久化 - RDB 与 AOF
Day116.尚医通:预约挂号详情 ※