如何在字符串数组中找到一个公平索引的字母

huangapple go评论73阅读模式
英文:

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&lt;arr.length; i++){
        String s = arr[i];
        char word = s.charAt(s);
      for(int j=0; j&lt;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
英文:
  1. 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.
  2. Iterate over the list/array, keep the index stored in some temporary variable, say firstArrayIndex
  3. 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
  4. Invoke int index = getMatchingIndex(firstItem, secondItem), if index &gt; -1, save the result like firstArrayIndex, 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 &lt; string1.length &amp;&amp; i &lt; 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 &gt; -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 &lt; arr.length - 1; firstIndex++) {
        firstString = arr[firstIndex];

        for (secondIndex = firstIndex + 1; secondIndex &lt; arr.length; secondIndex++) {
            secondString = arr[secondIndex];

            for (compareIndex = 0; compareIndex &lt; firstString.length(); compareIndex++) {

                if (firstString.charAt(compareIndex) == secondString.charAt(compareIndex)) {
                    return String.format(&quot;[%d,%d,%d]&quot;, firstIndex, secondIndex, compareIndex);
                }
            }
        }
    }

    return &quot;&quot;;
}

huangapple
  • 本文由 发表于 2020年9月29日 11:32:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64112479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定