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 1:
Input: text = " this is a sentence " Output: "this is a sentence" Explanation: There are a total of 9 spaces and 4 words. We can evenly divide the 9 spaces between the words: 9 / (4-1) = 3 spaces.
Example 2:
Input: text = " practice makes perfect" Output: "practice makes perfect " Explanation: There are a total of 7 spaces and 3 words. 7 / (3-1) = 3 spaces plus 1 extra space. We place this extra space at the end of the string.
Constraints:
1 <= text.length <= 100
text
consists of lowercase English letters and' '
.
text
contains at least one word.
풀이
은찬
/** * @param {string} text * @return {string} */ const reorderSpaces = (text) => { let answer = ""; let space = 0; let string = ""; const splitedText = []; for(let i = 0; i < text.length; i++){ if(text[i] === " "){ if(string.length){ splitedText.push(string); string = ""; } space++; } else{ string += text[i]; } } if(string.length){ splitedText.push(string); } if(!space){ return text; } const length = splitedText.length - 1 ? splitedText.length - 1 : 1; const q = Math.floor(space / length); const r = space % length; const sep = Array(q).fill(" ").join(""); const ext = Array(r).fill(" ").join(""); return splitedText.length === 1 ? splitedText[0] + sep : splitedText.join(sep) + (r ? ext : ""); };
효성
아니! 제 풀이 마음에 안들어요!
var reorderSpaces = function(text) { const splitSpace = text.split(''); const splitWord = text.split(' '); const wordArr = []; let wordCnt = 0, spaceCnt = 0; let answer = '', blank = ' '; splitSpace.forEach(el => { if(el === ' ') { spaceCnt++; } }); splitWord.forEach(el => { if(el !== '') { wordArr.push(el); wordCnt++; } }); if(wordCnt === 1) { return wordArr.pop() + blank.repeat(spaceCnt); } const value = parseInt(spaceCnt / (wordCnt - 1)); const left = spaceCnt % (wordCnt - 1); wordArr.forEach((el, i) => { if(i !== wordArr.length - 1) { const addWord = el + blank.repeat(value); answer += addWord; } else { answer += el + blank.repeat(left); } }); return answer; };
그래서 짧은 풀이 훔쳐왔어요.
var reorderSpaces = function(text) { let arr = text.split(" "); let totalSpace = arr.length-1; arr = arr.filter(w => w !== ''); let spaceBetween = arr.length > 1 ? Math.floor(totalSpace / (arr.length-1)) : 0; let spaceLeftOver = arr.length > 1 ? totalSpace % (arr.length-1) : totalSpace; return (arr.join(" ".repeat(spaceBetween)) + " ".repeat(spaceLeftOver)); // Time Complexity: O(n) // Space Complexity: O(n) };
재영
const makeSpaces = (cnt) => Array(cnt).fill(" ").join(""); const reorderSpaces = (text) => { let spaceNum = 0; let arr = text.split(" ").filter((val) => val); let spaceCnt = arr.length - 1; for (let i of text) { spaceNum += i === " "; } if (arr.length === 1) return arr[0] + makeSpaces(spaceNum); const maximizedSpaceCount = parseInt(spaceNum / spaceCnt); const remainder = spaceNum % spaceCnt; let result = ""; for (let i = 0; i < arr.length; i += 1) { result += arr[i] +(i !== arr.length - 1 ? makeSpaces(maximizedSpaceCount) : ""); } return result + makeSpaces(remainder); };