In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order
. The order
of the alphabet is some permutation of lowercase letters.
Given a sequence of words
written in the alien language, and the order
of the alphabet, return true
if and only if the given words
are sorted lexicographically in this alien language.
Example 1:
Input: words = [ "hello ", "leetcode "], order = "hlabcdefgijkmnopqrstuvwxyz " Output: true Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
Example 2:
Input: words = [ "word ", "world ", "row "], order = "worldabcefghijkmnpqstuvxyz " Output: false Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
Example 3:
Input: words = [ "apple ", "app "], order = "abcdefghijklmnopqrstuvwxyz " Output: false Explanation: The first three characters "app " match, and the second string is shorter (in size.) According to lexicographical rules "apple " > "app ", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).
Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
words[i]
and order
are English lowercase letters.program main
implicit none
integer :: i, j
character(len=26) :: order
character(len=20), allocatable :: words(:)
! read input
read(*,*) order
allocate(words(size(order)))
do i = 1, size(words)
read(*,*) words(i)
end do
! solve problem
call solve(words, order)
contains
subroutine solve(words, order)
implicit none
character(len=*), intent(in) :: words(:), order
logical :: sorted
sorted = .true.
do i = 1, size(words)-1
if (words(i+1) < words(i)) then
sorted = .false.
exit
end if
end do
write(*,*) sorted
end subroutine solve
end program main
temp.f95:10:24: 10 | allocate(words(size(order))) | 1 Error: ‘array’ argument of ‘size’ intrinsic at (1) must be an array
module main
implicit none
interface
function isAlienSorted(words, order) result(sorted)
character(len=*), intent(in) :: words(:)
character(len=*), intent(in) :: order
logical :: sorted
end function isAlienSorted
end interface
contains
subroutine run()
character(len=26) :: order
character(len=*), parameter :: words(3) = ["hello ", "leetcode ", "word "]
logical :: sorted
! Initialize the order of the alphabet
order = "hlabcdefgijkmnopqrstuvwxyz "
! Check if the words are sorted in the alien language
sorted = isAlienSorted(words, order)
! Print the result
if (sorted) then
print *, "The words are sorted in the alien language."
else
print *, "The words are not sorted in the alien language."
end if
end subroutine run
function isAlienSorted(words, order) result(sorted)
character(len=*), intent(in) :: words(:)
character(len=*), intent(in) :: order
logical :: sorted
integer :: i, j, k
! Initialize the result
sorted = .true.
! Check if the words are sorted lexicographically in the alien language
do i = 1, size(words) - 1
do j = 1, len_trim(words(i))
do k = 1, len_trim(words(i + 1))
if (words(i)(j:j) > words(i + 1)(k:k)) then
sorted = .false.
exit
else if (words(i)(j:j) < words(i + 1)(k:k)) then
exit
end if
end do
if (.not. sorted) exit
end do
if (.not. sorted) exit
end do
end function isAlienSorted
end module main
! Test the function with the given examples
program test
use main
implicit none
character(len=26) :: order
character(len=*), parameter :: words(3) = ["hello ", "leetcode ", "word "]
logical :: sorted
! Initialize the order of the alphabet
order = "hlabcdefgijkmnopqrstuvwxyz "
! Check if the words are sorted in the alien language
sorted = isAlienSorted(words, order)
! Print the result
if (sorted) then
print *, "The words are sorted in the alien language."
else
print *, "The words are not sorted in the alien language."
end if
end program test
temp.f95:19:56: 19 | character(len=*), parameter :: words(3) = ["hello ", "leetcode ", "word "] | 1 Error: Different CHARACTER lengths (6/9) in array constructor at (1) temp.f95:37:23: 7 | function isAlienSorted(words, order) result(sorted) | ...... 37 | function isAlienSorted(words, order) result(sorted) | 1 Error: Procedure ‘isaliensorted’ at (1) is already defined at (2) temp.f95:38:44: 38 | character(len=*), intent(in) :: words(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:39:41: 39 | character(len=*), intent(in) :: order | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:40:21: 40 | logical :: sorted | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:41:22: 41 | integer :: i, j, k | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:44:19: 44 | sorted = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:47:29: 47 | do i = 1, size(words) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:48:36: 48 | do j = 1, len_trim(words(i)) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:49:44: 49 | do k = 1, len_trim(words(i + 1)) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:50:29: 50 | if (words(i)(j:j) > words(i + 1)(k:k)) then | 1 Error: Syntax error in IF-expression at (1) temp.f95:51:36: 51 | sorted = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:52:24: 52 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:53:34: 53 | else if (words(i)(j:j) < words(i + 1)(k:k)) then | 1 Error: Missing ‘)’ in ELSE IF expression at (1) temp.f95:54:24: 54 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:55:19: 55 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:56:15: 56 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:57:34: 57 | if (.not. sorted) exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:58:11: 58 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:59:30: 59 | if (.not. sorted) exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:60:7: 60 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:62:3: 62 | end function isAlienSorted | 1 Error: Expecting END MODULE statement at (1) temp.f95:26:32: 26 | sorted = isAlienSorted(words, order) | 1 Error: Symbol ‘words’ at (1) has no IMPLICIT type temp.f95:68:9: 68 | use main | 1 Fatal Error: Cannot open module file ‘main.mod’ for reading at (1): No such file or directory compilation terminated.
def reverse_string(s):
return s[::-1]
The algorithm to reverse a string is simple. It takes the input string, iterates from the last character to the first, and appends each character to a new string, which is then returned.
In each of the provided implementations, the language's standard library, or a built-in method, is utilized to achieve the goal, rather than manually writing a loop to iterate over the string.
In C++, the std::reverse
function from the <algorithm>
library is used to reverse the input string.
In Java, a StringBuilder
is created from the input string, and then its built-in reverse
method is called. Finally, the reversed StringBuilder
is converted back to a String
.
In Python, slicing is used to reverse the string with the syntax s[::-1]
.
In JavaScript, the split
method is used to convert the string into an array of characters. Then, the reverse
method is called on the array, which reverses the order of the elements. Finally, the join
method is called on the reversed array to convert it back to a string.
#include <string>
#include <algorithm>
std::string reverseString(const std::string& s) {
std::string result = s;
std::reverse(result.begin(), result.end());
return result;
}
The algorithm to reverse a string is simple. It takes the input string, iterates from the last character to the first, and appends each character to a new string, which is then returned.
In each of the provided implementations, the language's standard library, or a built-in method, is utilized to achieve the goal, rather than manually writing a loop to iterate over the string.
In C++, the std::reverse
function from the <algorithm>
library is used to reverse the input string.
In Java, a StringBuilder
is created from the input string, and then its built-in reverse
method is called. Finally, the reversed StringBuilder
is converted back to a String
.
In Python, slicing is used to reverse the string with the syntax s[::-1]
.
In JavaScript, the split
method is used to convert the string into an array of characters. Then, the reverse
method is called on the array, which reverses the order of the elements. Finally, the join
method is called on the reversed array to convert it back to a string.