英文:
How to find a fair index's letter in an String array
问题
我想要找到在相同位置上重复的字母,并返回值 [第一个重复的数组,第二个重复的数组,重复的索引位置]
这里有一个例子。
String [] arr = ["cat","hat","foo"]
arr[0] = "cat" 而 arr[1] = "hat" 在索引 2 上有相同的字母 "t"。
所以你可以返回 0,1,2(重复的第一个数组,重复的第二个数组,重复的索引位置)
我尝试制作如下,我不能再做了..
for(int i=0; i<arr.length; i++){
String s = arr[i];
char word = s.charAt(s);
for(int j=0; j<arr[i].length(); j++){
}
}
英文:
I want to find duplicated letter in the same position and return the value [duplicated first arr, duplicated second arr, duplicated index position]
Here is an example.
String [] arr = ["cat","hat","foo"]
arr[0] = "cat" and arr[1] = "hat" has same letter "t" on index 2.
so you can return 0,1,2 ( duplicated first arr,duplicated second arr, duplicated index position)
I try to make below, I can't do anymore..
for(int i=0; i<arr.length; i++){
String s = arr[i];
char word = s.charAt(s);
for(int j=0; j<arr[i].length(); j++){
}
}
答案1
得分: 0
def getMatchingIndex(string1, string2):
index = -1
i = 0
while i < len(string1) and i < len(string2):
if string1[i] == string2[i]:
index = i
break
i += 1
return index
for idx1, firstItem in enumerate(arr):
index1 = idx1
for idx2, secondItem in enumerate(arr):
index2 = idx2
matchedIndex = getMatchingIndex(firstItem, secondItem)
if matchedIndex > -1:
# Save or return (idx1, idx2, matchedIndex) as needed
英文:
- Implement a method say
int getMatchingIndex(string1, string2)
which returns a valid index in case a match is found or return -1 in case no match is found. - Iterate over the list/array, keep the index stored in some temporary variable, say
firstArrayIndex
- For each item from Step 2, iterate over the whole list (if that is the intention) and keep the index stored in some temporary variable, say
secondArrayIndex
- Invoke
int index = getMatchingIndex(firstItem, secondItem)
, ifindex > -1
, save the result likefirstArrayIndex, secondArrayIndex, index
and keep iterative or return (as per the requirement)
Pseudo Code: (Not exactly but mix of code & pseudocode)
int getMatchingIndex(string1, string2) {
index = -1
i = 0
loop until i < string1.length && i < string2.length
if string1[i] == string2[i]
index = i
break
return index;
}
loop arr with index as idx1
index1 = idx1
loop arr with index as idx2
index2 = idx2
matchedIndex = getMatchingIndex(arr[idx1], arr[idx2]
if matchedIndex > -1
save or return (idx1, idx2, matchedIndex)
答案2
得分: 0
private String findFirstFairIndex(String[] arr) {
String firstString, secondString;
int firstIndex, secondIndex, compareIndex;
for (firstIndex = 0; firstIndex < arr.length - 1; firstIndex++) {
firstString = arr[firstIndex];
for (secondIndex = firstIndex + 1; secondIndex < arr.length; secondIndex++) {
secondString = arr[secondIndex];
for (compareIndex = 0; compareIndex < firstString.length(); compareIndex++) {
if (firstString.charAt(compareIndex) == secondString.charAt(compareIndex)) {
return String.format("[%d,%d,%d]", firstIndex, secondIndex, compareIndex);
}
}
}
}
return "";
}
英文:
Try this:
private String findFirstFairIndex(String[] arr) {
String firstString, secondString;
int firstIndex, secondIndex, compareIndex;
for (firstIndex = 0; firstIndex < arr.length - 1; firstIndex++) {
firstString = arr[firstIndex];
for (secondIndex = firstIndex + 1; secondIndex < arr.length; secondIndex++) {
secondString = arr[secondIndex];
for (compareIndex = 0; compareIndex < firstString.length(); compareIndex++) {
if (firstString.charAt(compareIndex) == secondString.charAt(compareIndex)) {
return String.format("[%d,%d,%d]", firstIndex, secondIndex, compareIndex);
}
}
}
}
return "";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论