Reverse the Odd-Length Words in a String

easy

By - Aman Pareek

Last Updated - 28/08/2024

Problem Statement

Write a function that takes a sentence as input and reverses each word with an odd length while leaving words with an even length unchanged. The function should maintain the original word order and spacing.

Example 1

Input: str = "This is a test sentence with odd and even length words"

Output: "This is a test sentence with ddo dna even length sdrow"

Example 2

Input: str = "Hello world"

Output: "olleH dlrow"

Constraints

  • The input string will not have leading or trailing spaces.

  • Words are defined as sequences of non-space characters separated by single spaces.

  • There will be no extra spaces between words in the input string.

Solution 1: Using split, map, and join

function reverseOddLengthWordsUsingSplitMapJoin(sentence) {
  return sentence
    .split(" ")
    .map(word => word.length % 2 === 1 ? word.split("").reverse().join("") : word)
    .join(" ");
} 

const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingSplitMapJoin(str1);  //output: This is a test sentence with ddo dna even length sdrow 

const str2 = "Hello world";
reverseOddLengthWordsUsingSplitMapJoin(str2);  //output: olleH dlrow 

Solution 2: Using for Loop

function reverseOddLengthWordsUsingForLoop(sentence) {
  const words = sentence.split(" ");
  for (let i = 0; i < words.length; i++) {
    if (words[i].length % 2 === 1) {
      words[i] = words[i].split("").reverse().join("");
    }
  }
  return words.join(" ");
} 

const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingForLoop(str1);  //output: This is a test sentence with ddo dna even length sdrow 

const str2 = "Hello world";
reverseOddLengthWordsUsingForLoop(str2);  //output: olleH dlrow 

Solution 3: Using for...of Loop

function reverseOddLengthWordsUsingForOfLoop(sentence) {
  const words = sentence.split(" ");
  const reversedWords = [];
  for (const word of words) {
    if (word.length % 2 === 1) {
      reversedWords.push(word.split("").reverse().join(""));
    } else {
      reversedWords.push(word);
    }
  }
  return reversedWords.join(" ");
} 

const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingForOfLoop(str1);  //output: This is a test sentence with ddo dna even length sdrow 

const str2 = "Hello world";
reverseOddLengthWordsUsingForOfLoop(str2);  //output: olleH dlrow 

Solution 4: Using reduce

function reverseOddLengthWordsUsingReduce(sentence) {
  return sentence
    .split(" ")
    .reduce((acc, word) => {
      const reversedWord = word.length % 2 === 1 ? word.split("").reverse().join("") : word;
      return acc + (acc ? " " : "") + reversedWord;
    }, "");
} 

const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingReduce(str1);  //output: This is a test sentence with ddo dna even length sdrow 

const str2 = "Hello world";
reverseOddLengthWordsUsingReduce(str2);  //output: olleH dlrow 

Solution 5: Using Array.from and map

function reverseOddLengthWordsUsingArrayFrom(sentence) {
  return Array.from(sentence.split(" "))
    .map(word => word.length % 2 === 1 ? Array.from(word).reverse().join("") : word)
    .join(" ");
} 

const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingArrayFrom(str1);  //output: This is a test sentence with ddo dna even length sdrow 

const str2 = "Hello world";
reverseOddLengthWordsUsingArrayFrom(str2);  //output: olleH dlrow 

Solution 6: Using split and Manual String Reversal

function reverseOddLengthWordsUsingManualReversal(sentence) {
  return sentence
    .split(" ")
    .map(word => {
      if (word.length % 2 === 1) {
        let reversedWord = "";
        for (let i = word.length - 1; i >= 0; i--) {
          reversedWord += word[i];
        }
        return reversedWord;
      }
      return word;
    })
    .join(" ");
} 

const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingManualReversal(str1);  //output: This is a test sentence with ddo dna even length sdrow 

const str2 = "Hello world";
reverseOddLengthWordsUsingManualReversal(str2);  //output: olleH dlrow 

Solution 7: Using split, map, and String.prototype.replace

function reverseOddLengthWordsUsingReplace(sentence) {
  return sentence.replace(/\S+/g, word => word.length % 2 === 1 ? word.split("").reverse().join("") : word);
} 

const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingReplace(str1);  //output: This is a test sentence with ddo dna even length sdrow 

const str2 = "Hello world";
reverseOddLengthWordsUsingReplace(str2);  //output: olleH dlrow 

Solution 8: Using map with split and reverse

function reverseOddLengthWordsUsingMapSplitReverse(sentence) {
  return sentence
    .split(" ")
    .map(word => word.length % 2 === 1 ? [...word].reverse().join("") : word)
    .join(" ");
} 

const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingMapSplitReverse(str1);  //output: This is a test sentence with ddo dna even length sdrow 

const str2 = "Hello world";
reverseOddLengthWordsUsingMapSplitReverse(str2);  //output: olleH dlrow 

Solution 9: Using Array.prototype.forEach

function reverseOddLengthWordsUsingForEach(sentence) {
  const words = sentence.split(" ");
  words.forEach((word, index) => {
    if (word.length % 2 === 1) {
      words[index] = word.split("").reverse().join("");
    }
  });
  return words.join(" ");
} 

const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingForEach(str1);  //output: This is a test sentence with ddo dna even length sdrow 

const str2 = "Hello world";
reverseOddLengthWordsUsingForEach(str2);  //output: olleH dlrow 

Solution 10: Using a Linked List Approach

function reverseOddLengthWordsUsingLinkedList(sentence) {
  const words = sentence.split(" ");
  const reversedWords = [];
  for (const word of words) {
    if (word.length % 2 === 1) {
      let reversedWord = "";
      for (let i = word.length - 1; i >= 0; i--) {
        reversedWord += word[i];
      }
      reversedWords.push(reversedWord);
    } else {
      reversedWords.push(word);
    }
  }
  return reversedWords.join(" ");
} 

const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingLinkedList(str1);  //output: This is a test sentence with ddo dna even length sdrow 

const str2 = "Hello world";
reverseOddLengthWordsUsingLinkedList(str2);  //output: olleH dlrow 

Popular Solutions