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

边栏推荐
- to-be-read list
- LeetCode Review Diary: 153. Find the Minimum Value in a Rotated Sort Array
- 搜罗汇总的效应
- Day116.尚医通:预约挂号详情 ※
- 乱七八糟的网站
- LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
- 3.Bean的作用域与生命周期
- 秒懂大模型 | 3步搞定AI写摘要
- HSDC is related to Independent Spanning Tree
- [LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
猜你喜欢

Use baidu EasyDL implement factory workers smoking behavior recognition

用位运算为你的程序加速

【LeetCode每日一题】——103.二叉树的锯齿形层序遍历

"NetEase Internship" Weekly Diary (2)

6-25 Vulnerability Exploitation - irc Backdoor Exploitation

Analysis of volatile principle

Data transfer at the data link layer

typescript30-any类型

使用百度EasyDL实现厂区工人抽烟行为识别

Garbage Collector CMS and G1
随机推荐
Constructor of typescript35-class
软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言
volatile原理解析
成都openGauss用户组招募啦!
nacos启动报错,已配置数据库,单机启动
"NetEase Internship" Weekly Diary (1)
YGG Guild Development Plan Season 1 Summary
LeetCode brushing diary: 33. Search and rotate sorted array
用位运算为你的程序加速
¶ Backtop back to the top is not effective
Day116.尚医通:预约挂号详情 ※
【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
typescript30 - any type
Golang分布式应用之Redis
Force buckle, 752-open turntable lock
ofstream,ifstream,fstream读写文件
Rust P2P Network Application Combat-1 P2P Network Core Concepts and Ping Program
Hash collisions and consistent hashing
Analysis of volatile principle
Understand the big model in seconds | 3 steps to get AI to write a summary