You are given an array of strings words
(0-indexed).
In one operation, pick two distinct indices i
and j
, where words[i]
is a non-empty string, and move any character from words[i]
to any position in words[j]
.
Return true
if you can make every string in words
equal using any number of operations, and false
otherwise.
Example 1:
Input: words = [ "abc ", "aabc ", "bc "]
Output: true
Explanation: Move the first 'a' in words[1] to the front of words[2], to make
words[1]
= "abc " and words[2] = "abc ".
All the strings are now equal to "abc ", so return true
.
Example 2:
Input: words = [ "ab ", "a "] Output: false Explanation: It is impossible to make all the strings equal using the operation.
Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 100
words[i]
consists of lowercase English letters.program main
implicit none
integer :: i, j
logical :: result
character(len=:), allocatable :: words(:)
words = ["abc ", "aabc ", "bc "]
result = canMakeEqual(words)
print "(L1)", result
words = ["ab ", "a "]
result = canMakeEqual(words)
print "(L1)", result
contains
function canMakeEqual(words) result(result)
implicit none
character(len=:), allocatable :: words(:)
logical :: result
integer :: i, j
do i = 1, size(words) - 1
do j = i + 1, size(words)
if (words(i) /= "" .and. words(j) /= "") then
exit
end if
end do
if (j == size(words)) then
cycle
end if
do while (words(i) /= words(j))
call moveChar(words, i, j)
end do
end do
result = all(words == words(1))
end function canMakeEqual
subroutine moveChar(words, i, j)
implicit none
character(len=:), allocatable :: words(:)
integer :: i, j
character :: c
c = words(i)(1:1)
words(i) = words(i)(2:) // c
words(j) = c // words(j)
end subroutine moveChar
end program main
temp.f95:8:20: 8 | words = ["abc ", "aabc ", "bc "] | 1 Error: Different CHARACTER lengths (4/5) in array constructor at (1) temp.f95:12:19: 12 | words = ["ab ", "a "] | 1 Error: Different CHARACTER lengths (3/2) in array constructor at (1)
program Equal_Strings
implicit none
integer, parameter :: MAX_LENGTH = 100
integer :: n, i, j, k
character(len=MAX_LENGTH) :: words(MAX_LENGTH)
logical :: equal
! Read the number of words
read(*,*) n
! Read the words
do i = 1, n
read(*,*) words(i)
end do
! Initialize the equal flag
equal = .true.
! Check if all the words are equal
do i = 1, n-1
do j = i+1, n
if (words(i) /= words(j)) then
equal = .false.
exit
end if
end do
if (.not. equal) exit
end do
! If all the words are equal, print "True"
! Otherwise, print "False"
if (equal) then
print *, "True"
else
print *, "False"
end if
end program Equal_Strings
At line 10 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7ea7d1385960 in ??? #1 0x7ea7d13864d9 in ??? #2 0x7ea7d15da17b in ??? #3 0x7ea7d15d3684 in ??? #4 0x7ea7d15d42aa in ??? #5 0x56324915e22c in MAIN__ #6 0x56324915e4e1 in main
def longestPalindromeSubseq(word1, word2):
n, m = len(word1), len(word2)
dp = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(n):
for j in range(m):
if word1[i] == word2[j]:
dp[i + 1][j + 1] = dp[i][j] + 1
else:
dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j])
return dp[n][m]
dp
of size (word1.length + 1) x (word2.length + 1) to store the longest common subsequence for each prefix of word1 and word2.dp
to 0.i
in word1 and j
in word2 are equal, update dp[i + 1][j + 1]
to dp[i][j] + 1
.dp[i + 1][j + 1]
to the maximum of dp[i][j + 1]
and dp[i + 1][j]
.dp[word1.length][word2.length]
, which is the length of the longest palindrome subsequence between word1 and word2.#include <string>
using namespace std;
int longestPalindromeSubseq(string word1, string word2) {
int n = word1.length();
int m = word2.length();
int dp[1001][1001] = {0};
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (word1[i] == word2[j]) {
dp[i + 1][j + 1] = dp[i][j] + 1;
} else {
dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
}
}
}
return dp[n][m];
}
dp
of size (word1.length + 1) x (word2.length + 1) to store the longest common subsequence for each prefix of word1 and word2.dp
to 0.i
in word1 and j
in word2 are equal, update dp[i + 1][j + 1]
to dp[i][j] + 1
.dp[i + 1][j + 1]
to the maximum of dp[i][j + 1]
and dp[i + 1][j]
.dp[word1.length][word2.length]
, which is the length of the longest palindrome subsequence between word1 and word2.