[LeetCode] 2451. Odd String Difference

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.

  • For example, for the string "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 <= 100
  • n == words[i].length
  • 2 <= n <= 20
  • words[i] consists of lowercase English letters.

差值数组不同的字符串。

给你一个字符串数组 words ,每一个字符串长度都相同,令所有字符串的长度都为 n 。

每个字符串 words[i] 可以被转化为一个长度为 n - 1 的 差值整数数组 difference[i] ,其中对于 0 <= j <= n - 2 有 difference[i][j] = words[i][j+1] - words[i][j] 。注意两个字母的差值定义为它们在字母表中 位置 之差,也就是说 'a' 的位置是 0 ,'b' 的位置是 1 ,'z' 的位置是 25 。

比方说,字符串 "acb" 的差值整数数组是 [2 - 0, 1 - 2] = [2, -1] 。
words 中所有字符串 除了一个字符串以外 ,其他字符串的差值整数数组都相同。你需要找到那个不同的字符串。

请你返回 words中 差值整数数组 不同的字符串。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/odd-string-difference
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是用 hashmap 记录不同的差值整数数组和对应的出现次数。如果有一个差值整数数组只出现过一次,那么他对应的那个字符串就是题目要找的字符串。这里我将差值整数数组转换成字符串,然后把这个字符串当做 key 存入 hashmap。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public String oddString(String[] words) {
 3         int n = words[0].length();
 4         HashMap<String, List<String>> map = new HashMap<>();
 5         for (String word : words) {
 6             String str = helper(word);
 7             if (!map.containsKey(str)) {
 8                 map.put(str, new ArrayList<>());
 9             }
10             map.get(str).add(word);
11         }
12 
13         for (String str : map.keySet()) {
14             if (map.get(str).size() == 1) {
15                 return map.get(str).get(0);
16             }
17         }
18         return "";
19     }
20 
21     private String helper(String word) {
22         int len = word.length();
23         StringBuilder sb = new StringBuilder();
24         for (int i = 1; i < len; i++) {
25             int diff = word.charAt(i) - word.charAt(i - 1);
26             sb.append((char) (diff + '0'));
27         }
28         return sb.toString();
29     }
30 }

 

LeetCode 题目总结

posted @ 2023-05-25 06:45  CNoodle  阅读(28)  评论(0编辑  收藏  举报