当前位置:网站首页>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();
}
}
}

边栏推荐
- 飞桨开源社区季度报告来啦,你想知道的都在这里
- 用位运算为你的程序加速
- AntPathMatcher uses
- Personal blog system project test
- Force buckle, 752-open turntable lock
- C language inserted into the characters of simple exercises
- LeetCode刷题日记:153、寻找旋转排序数组中的最小值
- Handwritten Blog Platform ~ Day Two
- Basic use of typescript34-class
- Constructor instance method inheritance of typescript37-class (extends)
猜你喜欢

用位运算为你的程序加速

"NetEase Internship" Weekly Diary (1)

typescript31-any类型

Fly propeller power space future PIE - Engine Engine build earth science

LeetCode brush diary: LCP 03. Machine's adventure

oracle查询扫描全表和走索引

ofstream,ifstream,fstream读写文件

飞桨助力航天宏图PIE-Engine地球科学引擎构建

Data transfer at the data link layer

Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
随机推荐
swift项目,sqlcipher3 -&gt; 4,无法打开旧版数据库有办法解决吗
Redis Subscription and Redis Stream
typescript34-class的基本使用
leetcode/字符串中的变位词-s1字符串的某个排列是s2的子串
手写一个博客平台~第三天
LeetCode Brushing Diary: 74. Searching 2D Matrix
HSDC和独立生成树相关
Win Go开发包安装配置、GoLand配置
[LeetCode Daily Question]——654. The largest binary tree
优炫数据库导库导错了能恢复吗?
typescript35-class的构造函数
超大规模的产业实用语义分割数据集PSSL与预训练模型开源啦!
【LeetCode Daily Question】——704. Binary Search
『网易实习』周记(二)
Ask God to answer, how should this kind of sql be written?
Day116.尚医通:预约挂号详情 ※
nacos启动报错,已配置数据库,单机启动
手写博客平台~第二天
LeetCode 213. Robbery II (2022.08.01)
LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置