Skip to main content

Command Palette

Search for a command to run...

Generate a String With Characters That Have Odd Counts

Published
1 min read
Generate a String With Characters That Have Odd Counts
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Given an integer n, return a string with n characters such that each character in such string occurs an odd number of times.

The returned string must contain only lowercase English letters. If there are multiples valid strings, return any of them.

LeetCode Problem - 1374

class Solution {
    // Method to generate a string of length 'n' such that all characters in the string are either 'a' or 'b'
    // The string must have an odd length if 'n' is odd, and an even length if 'n' is even
    public String generateTheString(int n) {
        // Use StringBuilder to efficiently build the resulting string
        StringBuilder sb = new StringBuilder();

        // Check if 'n' is even
        if (n % 2 == 0) {
            // If 'n' is even, create a string with (n-1) 'a' characters and 1 'b' character
            sb.append("a".repeat(n - 1));  // Append (n-1) 'a' characters
            sb.append("b");  // Append 1 'b' character to make the total length 'n'
        } else {
            // If 'n' is odd, create a string with 'n' 'a' characters
            sb.append("a".repeat(n));  // Append 'n' 'a' characters
        }

        // Convert the StringBuilder to a string and return it
        return sb.toString();
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.