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

边栏推荐
- 力扣 1161. 最大层内元素和
- Hash collisions and consistent hashing
- LeetCode刷题日记: 33、搜索旋转排序数组
- Constructor of typescript35-class
- 个人博客系统项目测试
- leetcode/字符串中的变位词-s1字符串的某个排列是s2的子串
- 6-24 exploit-vnc password cracking
- Hiring a WordPress Developer: 4 Practical Ways
- Software testing Interface automation testing Pytest framework encapsulates requests library Encapsulates unified request and multiple base path processing Interface association encapsulation Test cas
- typescript38-class的构造函数实例方法继承(implement)
猜你喜欢

Day115.尚医通:后台用户管理:用户锁定解锁、详情、认证列表审批

密码学的基础:X.690和对应的BER CER DER编码
![[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree](/img/b9/35813ae2972375fa728e3c11fab5d3.png)
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree

记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换

【服务器数据恢复】服务器Raid5阵列mdisk磁盘离线的数据恢复案例

typescript29-枚举类型的特点和原理

垃圾回收器CMS和G1

The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!

YGG Guild Development Plan Season 1 Summary

Use baidu EasyDL implement factory workers smoking behavior recognition
随机推荐
力扣 1374. 生成每种字符都是奇数个的字符串
Golang分布式应用之Redis
C language inserted into the characters of simple exercises
记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
LeetCode brush diary: LCP 03. Machine's adventure
typescript29-枚举类型的特点和原理
Simple example of libcurl accessing url saved as file
nacos启动报错,已配置数据库,单机启动
to-be-read list
MySQL optimization strategy
¶ Backtop back to the top is not effective
Centos7 安装postgresql并开启远程访问
Hash collisions and consistent hashing
libcurl访问url保存为文件的简单示例
LeetCode brushing diary: 53, the largest sub-array and
用位运算为你的程序加速
Speed up your programs with bitwise operations
"NetEase Internship" Weekly Diary (2)
Constructor instance method inheritance of typescript38-class (implement)