|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=1374 lang=java |
| 3 | + * |
| 4 | + * [1374] Generate a String With Characters That Have Odd Counts |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Easy (77.22%) |
| 10 | + * Likes: 12 |
| 11 | + * Dislikes: 53 |
| 12 | + * Total Accepted: 9.1K |
| 13 | + * Total Submissions: 11.8K |
| 14 | + * Testcase Example: '4' |
| 15 | + * |
| 16 | + * Given an integer n, return a string with n characters such that each |
| 17 | + * character in such string occurs an odd number of times. |
| 18 | + * |
| 19 | + * The returned string must contain only lowercase English letters. If there |
| 20 | + * are multiples valid strings, return any of them. |
| 21 | + * |
| 22 | + * |
| 23 | + * Example 1: |
| 24 | + * |
| 25 | + * |
| 26 | + * Input: n = 4 |
| 27 | + * Output: "pppz" |
| 28 | + * Explanation: "pppz" is a valid string since the character 'p' occurs three |
| 29 | + * times and the character 'z' occurs once. Note that there are many other |
| 30 | + * valid strings such as "ohhh" and "love". |
| 31 | + * |
| 32 | + * |
| 33 | + * Example 2: |
| 34 | + * |
| 35 | + * |
| 36 | + * Input: n = 2 |
| 37 | + * Output: "xy" |
| 38 | + * Explanation: "xy" is a valid string since the characters 'x' and 'y' occur |
| 39 | + * once. Note that there are many other valid strings such as "ag" and "ur". |
| 40 | + * |
| 41 | + * |
| 42 | + * Example 3: |
| 43 | + * |
| 44 | + * |
| 45 | + * Input: n = 7 |
| 46 | + * Output: "holasss" |
| 47 | + * |
| 48 | + * |
| 49 | + * |
| 50 | + * Constraints: |
| 51 | + * |
| 52 | + * |
| 53 | + * 1 <= n <= 500 |
| 54 | + * |
| 55 | + */ |
| 56 | + |
| 57 | +// @lc code=start |
| 58 | +class Solution { |
| 59 | + public String generateTheString(int n) { |
| 60 | + String ret = ""; |
| 61 | + for(int i=0; i<n-1; i++) |
| 62 | + ret += 'a'; |
| 63 | + if(n%2==0) // even |
| 64 | + ret += 'b'; |
| 65 | + else |
| 66 | + ret += 'a'; |
| 67 | + return ret; |
| 68 | + } |
| 69 | +} |
| 70 | +// @lc code=end |
0 commit comments