Heap Sort Algorithm
This is another very useful sorting algorithm based on Heap data structure. Read…
October 06, 2020
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.
Example
Input: text = " this is a sentence "
Output: "this is a sentence"
Input: text = " practice makes perfect"
Output: "practice makes perfect "
Input: text = "hello world"
Output: "hello world"
You need to have three things to solve this problem:
If you have 4 words, and 9 spaces. You will put 9 / (4-1) = 3
spaces in between.
Lets look at the algorithm:
There are some special conditions when
Lets look at the code now
private int process(String text, List<String> words) {
int spaces = 0;
for (int i=0; i<text.length(); ) {
if (text.charAt(i) == ' ') {
while (i < text.length() && text.charAt(i) == ' ') {
spaces ++;
i++;
}
}
else {
StringBuilder sb = new StringBuilder();
while (i < text.length() && text.charAt(i) != ' ') {
sb.append(text.charAt(i));
i++;
}
if (sb.length() > 0) {
words.add(sb.toString());
}
}
}
return spaces;
}
public String reorderSpaces(String text) {
List<String> words = new ArrayList<String>();
int spaces = this.process(text, words);
int divider = (words.size() - 1) > 0 ? (words.size() - 1) : 1;
int targetSpaces = spaces / divider;
int remainingSpaces = spaces % divider;
if (words.size()<= 1) {
remainingSpaces += targetSpaces;
}
StringBuilder sb = new StringBuilder();
if (words.size() > 0) {
sb.append(words.get(0));
}
for (int i=1; i<words.size(); i++) {
for (int j=0; j<targetSpaces; j++) {
sb.append(" ");
}
sb.append(words.get(i));
}
if (remainingSpaces > 0) {
for (int j=0; j<remainingSpaces; j++) {
sb.append(" ");
}
}
return sb.toString();
}
Its O(n)
This is another very useful sorting algorithm based on Heap data structure. Read…
Problem Statement Replace all spaces in a string with ‘%20’ (three characters…
Problem Statement Given a non-empty array of digits representing a non-negative…
Problem Statement Given an array of integers, find if the array contains any…
Its a tree based data structure which is a complete binary tree(all nodes have…
A number consists of digits. Example: 843. Its a 3-digit number. Radix sort…
Problem Statement You are given a rows x cols matrix grid. Initially, you are…
Problem Statement Given a string s, return the maximum number of unique…
Problem The Leetcode file system keeps a log each time some user performs a…
Problem Statement Replace all spaces in a string with ‘%20’ (three characters…
Problem Implement an algorithm to determine if a string has all the characters…
Problem Statement Given a string, determine if it is a palindrome, considering…