Strings with different difference arrays [LC2451]
Given an array of strings
words
, each of which has the same length, let the length of all strings ben
.Each string
words[i]
can be converted into an integer array of difference valuesn - 1
of length , where there is for . Note that the difference between two letters is defined as the difference between their positions , that is , the position of is , the position of is , the position of is .difference[i]
0 <= j <= n - 2
difference[i][j] = words[i][j+1] - words[i][j]
'a'
0
'b'
1
'z'
25
- Let's say the array of integers for the difference
"acb"
of is[2 - 0, 1 - 2] = [2, -1]
.
words
All strings in except one have the same array of difference integers. You need to find that different string.Please
words
return the different strings in the difference integer array .
Longitudinal Comparison Realization 1
-
train of thought
Since
words
all strings in except one have the same array of difference integers. Therefore, we can find the difference array of the first string, and record the number of occurrences of the difference array count. Then enumerate other strings, compare whether the difference arrays are the same, and discuss according to the situation.- If the difference arrays are different, and
count>1
, then the string at this time is the answer. - However, there is still a situation where the first string is the answer, so it
count
will always be 1 in the end.
- If the difference arrays are different, and
-
accomplish
class Solution { public String oddString(String[] words) { // 先记录第一个字符串的差值数组,并记录该差值数组出现的次数count // 再记录与它不同的差值数组的下标diff // 最后如果count>1,那么返回diff,否则返回0 int n = words[0].length(); int[] array = new int[n - 1]; int count = 1, index = -1; for (int i = 0; i < n - 1; i++){ array[i] = words[0].charAt(i + 1) - words[0].charAt(i); } for (int j = 1; j < words.length; j++){ boolean same = true; for (int i = 0; i < n - 1; i++){ if (words[j].charAt(i + 1) - words[j].charAt(i) != array[i]){ same = false; index = j; break; } } if (same) count++; if (count > 1 && index != -1) return words[index]; } return words[0]; } }
- the complexity
- Time complexity: O ( m ∗ n ) \mathcal{O}(m*n)O(m∗n ) , n is the length of the string, m is the number of strings
- Space complexity: O ( n ) \mathcal{O}(n)O ( n )
- the complexity
Longitudinal Comparison Implementation 2
-
Ideas:
It is also to first record the difference array of the first string, and then find a string different from it, then the answer is to choose one of the two. Then select another string to compare with the difference array of the first string, if they are different, the answer is the first string, otherwise it is another string
-
accomplish
class Solution { public String oddString(String[] words) { int n = words.length; int i = 1; for(i = 1; i < n; i++){ if(!check(words[i], words[0])){ break; } } for(int j = 1; j < n; j++){ if(j != i){ if(check(words[j], words[0])){ return words[i]; } else{ return words[0]; } } } return ""; } private boolean check(String s1, String s2){ int n = s1.length(); int m = s1.charAt(0) - s2.charAt(0); for(int i = 1; i < n; i++){ if(s1.charAt(i) - s2.charAt(i) != m){ return false; } } return true; } } 作者:一只粗糙的疯子 链接:https://leetcode.cn/problems/odd-string-difference/solutions/2282834/chai-zhi-shu-zu-bu-tong-de-zi-fu-chuan-b-siph/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。