You are given an array of equal-length strings words. Assume that the length of each string is n.
Each string words[i] can be converted into a difference integer array difference[i] of length n - 1 where difference[i][j] = words[i][j+1] - words[i][j] where 0 <= j <= n - 2. Note that the difference between two letters is the difference between their positions in the alphabet i.e. the position of 'a' is 0, 'b' is 1, and 'z' is 25.
"acb ", the difference integer array is [2 - 0, 1 - 2] = [2, -1].All the strings in words have the same difference integer array, except one. You should find that string.
Return the string in words that has different difference integer array.
Example 1:
Input: words = [ "adc ", "wzy ", "abc "] Output: "abc " Explanation: - The difference integer array of "adc " is [3 - 0, 2 - 3] = [3, -1]. - The difference integer array of "wzy " is [25 - 22, 24 - 25]= [3, -1]. - The difference integer array of "abc " is [1 - 0, 2 - 1] = [1, 1]. The odd array out is [1, 1], so we return the corresponding string, "abc ".
Example 2:
Input: words = [ "aaa ", "bob ", "ccc ", "ddd "] Output: "bob " Explanation: All the integer arrays are [0, 0] except for "bob ", which corresponds to [13, -13].
Constraints:
3 <= words.length <= 100n == words[i].length2 <= n <= 20words[i] consists of lowercase English letters.program main
implicit none
character(len=:), allocatable :: words(:)
integer :: n
integer :: i
words = ["adc ", "wzy ", "abc "]
n = len(words(1))
do i = 1, size(words)
print '(A)', solve(words(i))
end do
contains
function solve(word) result(diff_str)
implicit none
character(len=*), intent(in) :: word
integer :: diff(size(word)-1)
integer :: i
character(len=:), allocatable :: diff_str
diff = [(word(i+1:i+2) - word(i:i), i=1, size(word)-1)]
write (unit=diff_str, fmt='(*(I0, A))') diff
end function solve
end program main
temp.f95:20:29:
20 | integer :: diff(size(word)-1)
| 1
Error: โarrayโ argument of โsizeโ intrinsic at (1) must be an array
temp.f95:20:29:
20 | integer :: diff(size(word)-1)
| 1
Error: โarrayโ argument of โsizeโ intrinsic at (1) must be an array
temp.f95:24:54:
24 | diff = [(word(i+1:i+2) - word(i:i), i=1, size(word)-1)]
| 1
Error: โarrayโ argument of โsizeโ intrinsic at (1) must be an array
temp.f95:24:17:
24 | diff = [(word(i+1:i+2) - word(i:i), i=1, size(word)-1)]
| 1
Error: Operands of binary numeric operator โ-โ at (1) are CHARACTER(*)/CHARACTER(*)
program main
implicit none
integer :: i, j, n, len
character(len=20) :: words(100), difference(100, 20)
character(len=20) :: odd_out
! Get the length of the strings
n = len(words(1))
! Initialize the difference array
do i = 1, n - 1
difference(:, i) = 0
end do
! Calculate the difference array for each string
do i = 1, size(words)
do j = 1, n - 1
difference(i, j) = words(i)(j+1:j+1) - words(i)(j:j)
end do
end do
! Find the odd out
odd_out = words(1)
do i = 2, size(words)
if (any(difference(i, :) /= difference(i-1, :))) then
odd_out = words(i)
exit
end if
end do
! Print the result
write (*, *) "The odd out is: ", odd_out
end program
temp.f95:14:23:
14 | difference(:, i) = 0
| 1
Error: Cannot convert INTEGER(4) to CHARACTER(20) at (1)
temp.f95:20:27:
20 | difference(i, j) = words(i)(j+1:j+1) - words(i)(j:j)
| 1
Error: Operands of binary numeric operator โ-โ at (1) are CHARACTER(1)/CHARACTER(1)
def stringWithDifferentDifference(words):
def get_difference_array(word):
return [ord(word[i+1]) - ord(word[i]) for i in range(len(word) - 1)]
diffArrays = [get_difference_array(word) for word in words]
oddArrayIndex = -1
for i in range(1, len(diffArrays)):
matchCount = sum(diffArrays[0][j] == diffArrays[i][j] for j in range(len(diffArrays[i])))
if matchCount == 0:
oddArrayIndex = i
break
return words[oddArrayIndex]
The algorithm starts by creating a helper function getDifferenceArray, which creates an array containing the differences of consecutive characters in the given string. Then, for each string in the words array, I call the helper function to generate difference arrays and store them in a new array diffArrays.`
After that, a loop iterates over diffArrays, comparing each difference array with the first one in the array (diffArrays[0]). If there are no matches at all (meaning it's completely different from the first difference array), I set oddArrayIndex to that index and break the loop. Finally, I return the string in the words array at the index of oddArrayIndex.
#include <vector>
#include <string>
std::string stringWithDifferentDifference(std::vector<std::string>& words) {
auto getDifferenceArray = [](const std::string& word) {
std::vector<int> differenceArray;
for (size_t i = 1; i < word.size(); i++) {
differenceArray.push_back(word[i] - word[i - 1]);
}
return differenceArray;
};
std::vector<std::vector<int>> diffArrays;
for (const auto& word : words) {
diffArrays.push_back(getDifferenceArray(word));
}
int oddArrayIndex = -1;
for (int i = 1; i < diffArrays.size(); i++) {
int matchCount = 0;
for (int j = 0; j < diffArrays[i].size(); j++) {
if (diffArrays[0][j] == diffArrays[i][j]) {
matchCount++;
}
}
if (matchCount == 0) {
oddArrayIndex = i;
break;
}
}
return words[oddArrayIndex];
}
The algorithm starts by creating a helper function getDifferenceArray, which creates an array containing the differences of consecutive characters in the given string. Then, for each string in the words array, I call the helper function to generate difference arrays and store them in a new array diffArrays.`
After that, a loop iterates over diffArrays, comparing each difference array with the first one in the array (diffArrays[0]). If there are no matches at all (meaning it's completely different from the first difference array), I set oddArrayIndex to that index and break the loop. Finally, I return the string in the words array at the index of oddArrayIndex.