TreeviewCopyright © aleen42 all right reserved, powered by aleen42

1374. 生成每种字符都是奇数个的字符串

https://leetcode-cn.com/problems/generate-a-string-with-characters-that-have-odd-counts/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-08-29 16:02:28
 * @LastEditTime: 2020-08-29 16:02:51
 * @Description: https://leetcode-cn.com/problems/generate-a-string-with-characters-that-have-odd-counts/
 * @FilePath: \leetcode-googtech\#1374. Generate a String With Characters That Have Odd Counts\Solution.java
 * @WebSite: https://algorithm.show/
 */

class Solution {
    public String generateTheString(int n) {
        StringBuilder sb = new StringBuilder();
        if(n % 2 == 0) {
            sb.append('a');
            n--;
        }
        for(int i = 0; i < n; i++) {
            sb.append('b');
        }
        return sb.toString();
    }
}

Python

'''
Author: Goog Tech
Date: 2020-08-29 16:02:32
LastEditTime: 2020-08-29 16:02:44
Description: https://leetcode-cn.com/problems/generate-a-string-with-characters-that-have-odd-counts/
FilePath: \leetcode-googtech\#1374. Generate a String With Characters That Have Odd Counts\Solution.py
WebSite: https://algorithm.show/
'''

class Solution(object):
    def generateTheString(self, n):
        """
        :type n: int
        :rtype: str
        """
        return "a" * n if n % 2 != 0 else "a" * (n - 1) + "b";
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""