---
# System prepended metadata

title: 2451. Odd String Difference
tags: [Leetcode, Easy]

---

# 2451. Odd String Difference
###### tags: `Leetcode` `Easy`
Link: https://leetcode.com/problems/odd-string-difference/description/
## 思路 $O(MN)$ $O(1)$
对于每个位置```j+1```和```j```遍历所有字符串 把出现的```words[i][j+1]-words[i][j]```和对应的```i```存在map里面 遍历完所有字符串之后如果map的size等于2 说明有两个不同的```words[i][j+1]-words[i][j]``` 找到对应的list size为1的 他就是答案
## Code
```java=
class Solution {
    public String oddString(String[] words) {
        int n = words.length, len = words[0].length();
        Map<Integer, List<Integer>> map = new HashMap<>();
        for(int j=0; j<=len-2; j++){
            for(int i=0; i<n; i++){
                int diff = words[i].charAt(j+1)-words[i].charAt(j);
                if(!map.containsKey(diff)) map.put(diff, new ArrayList<>());
                map.get(diff).add(i);
            }
            if(map.size()==2){
                for(int key:map.keySet()){
                    if(map.get(key).size()==1) return words[map.get(key).get(0)];
                }
            }
            map.clear();
        }
        return words[0];
    }
}
```