Reverse Words in a String III

🏠 ⬅️ ➡️

Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"

Example 2:

Input: s = "God Ding" Output: "doG gniD"

Constraints:

  • 1 <= s.length <= 5 * 104
  • s contains printable ASCII characters.
  • s does not contain any leading or trailing spaces.
  • There is at least one word in s.
  • All the words in s are separated by a single space.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    character(len=:), allocatable :: s, reversed_string
    integer :: i, j, k

    ! Example 1
    s = "Let's take LeetCode contest"
    call reverse_words(s)
    write (*,*) 'Example 1:'
    write (*,'(A)') trim(adjustl(s)) // ' -> ' // trim(adjustl(reversed_string))

    ! Example 2
    s = "God Ding"
    call reverse_words(s)
    write (*,*) 'Example 2:'
    write (*,'(A)') trim(adjustl(s)) // ' -> ' // trim(adjustl(reversed_string))

contains

    subroutine reverse_words(s)
        implicit none
        character(len=*), intent(inout) :: s
        integer :: i, j

        do i = 1, len(s)
            if (s(i:i) == ' ') then
                cycle
            end if

            j = i
            do while (j < len(s) .and. s(j+1:j+1) /= ' ')
                j = j + 1
            end do

            reversed_string = s(i:j-1)
            call reverse_string(reversed_string)
            s(i:j-1) = reversed_string
        end do
    end subroutine reverse_words

    subroutine reverse_string(s)
        implicit none
        character(len=*), intent(inout) :: s
        integer :: i, j

        i = 1
        j = len(s)
        do while (i < j)
            s(i:i) = s(j:j)
            s(j:j) = s(i:i)
            i = i + 1
            j = j - 1
        end do
    end subroutine reverse_string

end program main
Compiled
Executed
Correct
!This script takes a string as input and reverses the order of characters in each word within a sentence while still preserving whitespace and initial word order.

program reverse_words
implicit none
character(len=*), parameter :: newline = char(10) // ASCII code for newline character
character(len=*) :: s
character(len=:), allocatable :: words(:)
integer :: i, j, k, n

!Read input string from stdin
read(*,*) s

!Split input string into individual words using whitespace as delimiter
words = trim(s)
words = words(1:len_trim(words)) // split string into individual words

!Reverse the order of characters in each word
do i = 1, size(words)
    words(i) = words(i)(len_trim(words(i)):1:-1) // reverse order of characters in each word
end do

!Join the reversed words back into a single string
n = 0
do i = 1, size(words)
    n = n + len_trim(words(i)) + 1
end do
allocate(character(len=n) :: s)
s = ''
do i = 1, size(words)
    s = trim(s) // words(i) // ' '
end do

!Print output string to stdout
write(*,*) s

end program reverse_words
🌐 Data from online sources
def reverse_words_in_string(s: str) -> str:
    return " ".join(word[::-1] for word in s.split(" "))

The algorithm first splits the input string s into words based on whitespace. Then, for each word, it reverses the order of characters and appends it to the output. Finally, the reversed words are joined with whitespace and returned as the result.

In each language implementation: - C++: The algorithm uses istringstream to split the string into words and ostringstream to create the output string. - Java: The algorithm splits the string using split, reverses each word using StringBuilder and then String.join to join the words with a space delimiter. - Python: The algorithm uses a list comprehension to reverse the words in the input string and join to concatenate them. - JavaScript: The algorithm uses split, map, and join operations on the input string to reverse each word and concatenate them.

🌐 Data from online sources
#include <string>
#include <algorithm>
#include <sstream>

std::string reverseWordsInString(const std::string& s) {
    std::istringstream iss(s);
    std::ostringstream oss;
    std::string word;
    while (iss >> word) {
        std::reverse(word.begin(), word.end());
        oss << word << ' ';
    }
    std::string result = oss.str();
    if (!result.empty()) result.pop_back();
    return result;
}

The algorithm first splits the input string s into words based on whitespace. Then, for each word, it reverses the order of characters and appends it to the output. Finally, the reversed words are joined with whitespace and returned as the result.

In each language implementation: - C++: The algorithm uses istringstream to split the string into words and ostringstream to create the output string. - Java: The algorithm splits the string using split, reverses each word using StringBuilder and then String.join to join the words with a space delimiter. - Python: The algorithm uses a list comprehension to reverse the words in the input string and join to concatenate them. - JavaScript: The algorithm uses split, map, and join operations on the input string to reverse each word and concatenate them.