Skip to main content

Command Palette

Search for a command to run...

Rearrange Spaces Between Words

Published
2 min read
Rearrange Spaces Between Words
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.

You are given a string text of words that are placed among some number of spaces. Each word consists of one or more lowercase English letters and are separated by at least one space. It's guaranteed that text contains at least one word.

Rearrange the spaces so that there is an equal number of spaces between every pair of adjacent words and that number is maximized. If you cannot redistribute all the spaces equally, place the extra spaces at the end, meaning the returned string should be the same length as text.

Return the string after rearranging the spaces.

LeetCode Problem - 1592

class Solution {
    public String reorderSpaces(String text) {
        // Step 1: Count the number of spaces in the input text
        int count = 0;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == ' ') {
                count++;
            }
        }

        // Step 2: Split the text into words, removing extra spaces
        String[] newStr = text.trim().replaceAll("\\s+", " ").split(" ");
        int length = newStr.length; // number of words

        // Step 3: Handle the edge case where there's only one word
        if (length == 1) {
            // If there's only one word, append all spaces at the end of the word
            return newStr[0] + " ".repeat(count);
        }

        // Step 4: Calculate the number of spaces to distribute between words
        int space = count / (length - 1); // spaces between words
        int remainingSpaces = count % (length - 1); // leftover spaces after even distribution

        // Step 5: Rebuild the string with evenly distributed spaces
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(newStr[i]); // append the word
            if (i < length - 1) {
                // Add spaces between words, except after the last word
                sb.append(" ".repeat(space));
            }
        }

        // Step 6: Append any remaining spaces at the end of the sentence
        sb.append(" ".repeat(remainingSpaces));

        // Return the final result
        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.