2531.使字符串中不同字符的数目相等
链接:2531.使字符串中不同字符的数目相等
难度:Medium
标签:哈希表、字符串、计数
简介:给你两个下标从 0 开始的字符串 word1 和 word2 。如果可以通过 恰好一次 移动,使 word1 和 word2 中不同字符的数目相等,则返回 true ;否则,返回 false 。
题解 1 - typescript
- 编辑时间:2023-01-08
- 执行用时:160ms
- 内存消耗:49MB
- 编程语言:typescript
- 解法介绍:存储后遍历。
const list = new Array(26).fill(0).map((_, i) => String.fromCodePoint(i + 97));
function isItPossible(word1: string, word2: string): boolean {
  const map1 = new Map<string, number>();
  for (const c of word1) map1.set(c, (map1.get(c) ?? 0) + 1);
  const map2 = new Map<string, number>();
  for (const c of word2) map2.set(c, (map2.get(c) ?? 0) + 1);
  const len1 = map1.size;
  const len2 = map2.size;
  for (const c1 of list) {
    for (const c2 of list) {
      if (check(c1, c2)) return true;
    }
  }
  return false;
  function check(c1: string, c2: string): boolean {
    const cnt1 = map1.get(c1) ?? 0;
    const t1 = map1.get(c2) ?? 0;
    const cnt2 = map2.get(c2) ?? 0;
    const t2 = map2.get(c1) ?? 0;
    if (cnt1 === 0 || cnt2 === 0) return false;
    if (c1 === c2) return len1 === len2;
    if (cnt1 === 1 && cnt2 === 1) {
      if (t1 === 0 && t2 === 0) return len1 === len2;
      if (t1 === 0 && t2 !== 0) return len1 + 1 === len2;
      if (t1 !== 0 && t2 === 0) return len1 === len2 + 1;
      if (t1 !== 0 && t2 !== 0) return len1 === len2;
    }
    if (cnt1 === 1 && cnt2 > 1) {
      if (t1 === 0 && t2 === 0) return len1 === len2 + 1;
      if (t1 === 0 && t2 !== 0) return len1 === len2;
      if (t1 !== 0 && t2 === 0) return len1 - 1 === len2 + 1;
      if (t1 !== 0 && t2 !== 0) return len1 - 1 === len2;
    }
    if (cnt1 > 1 && cnt2 === 1) {
      if (t1 === 0 && t2 === 0) return len2 === len1 + 1;
      if (t1 !== 0 && t2 === 0) return len2 === len1;
      if (t1 === 0 && t2 !== 0) return len2 - 1 === len1 + 1;
      if (t1 !== 0 && t2 !== 0) return len2 - 1 === len1;
    }
    if (cnt1 > 1 && cnt2 > 1) {
      if (t1 === 0 && t2 === 0) return len1 === len2;
      if (t1 !== 0 && t2 === 0) return len1 === len2 + 1;
      if (t1 === 0 && t2 !== 0) return len1 + 1 === len2;
      if (t1 !== 0 && t2 !== 0) return len1 === len2;
    }
    return false;
  }
}
题解 2 - rust
- 编辑时间:2023-01-08
- 执行用时:4ms
- 内存消耗:2.4MB
- 编程语言:rust
- 解法介绍:同上, 优化。
impl Solution {
    pub fn is_it_possible(word1: String, word2: String) -> bool {
        let mut list = [0; 26];
        for i in 0..26 {
            list[i] = i;
        }
        let (mut m1, mut len1) = ([0; 26], 0);
        let (mut m2, mut len2) = ([0; 26], 0);
        for c in word1.chars() {
            let idx = c as usize - 'a' as usize;
            m1[idx] += 1;
            if m1[idx] == 1 {
                len1 += 1;
            }
        }
        for c in word2.chars() {
            let idx = c as usize - 'a' as usize;
            m2[idx] += 1;
            if m2[idx] == 1 {
                len2 += 1;
            }
        }
        for i1 in list.iter() {
            for i2 in list.iter() {
                let (i1, i2) = (*i1, *i2);
                let (c1, t1) = (m1[i1], m1[i2]);
                let (c2, t2) = (m2[i2], m2[i1]);
                if c1 == 0 || c2 == 0 {
                    continue;
                } else if i1 == i2 {
                    if len1 == len2 {
                        return true;
                    }
                } else {
                    let mut len1 = len1;
                    let mut len2 = len2;
                    if c1 == 1 {
                        len1 -= 1;
                    }
                    if t1 == 0 {
                        len1 += 1;
                    }
                    if c2 == 1 {
                        len2 -= 1;
                    }
                    if t2 == 0 {
                        len2 += 1
                    }
                    if len1 == len2 {
                        return true;
                    }
                }
            }
        }
        false
    }
}
题解 3 - rust
- 编辑时间:2023-01-08
- 执行用时:4ms
- 内存消耗:2.3MB
- 编程语言:rust
- 解法介绍:同上。
impl Solution {
    pub fn is_it_possible(word1: String, word2: String) -> bool {
        let mut list = [0; 26];
        for i in 0..26 {
            list[i] = i;
        }
        let mut m1 = [0; 26];
        let mut m2 = [0; 26];
        let mut len1 = 0;
        let mut len2 = 0;
        for c in word1.chars() {
            let idx = c as usize - 'a' as usize;
            m1[idx] += 1;
            if m1[idx] == 1 {
                len1 += 1;
            }
        }
        for c in word2.chars() {
            let idx = c as usize - 'a' as usize;
            m2[idx] += 1;
            if m2[idx] == 1 {
                len2 += 1;
            }
        }
        let check = |i1: usize, i2: usize| -> bool {
            let c1 = m1[i1];
            let t1 = m1[i2];
            let c2 = m2[i2];
            let t2 = m2[i1];
            if c1 == 0 || c2 == 0 {
                return false;
            }
            if i1 == i2 {
                return len1 == len2;
            }
            if c1 == 1 && c2 == 1 {
                if t1 == 0 && t2 == 0 {
                    return len1 == len2;
                }
                if t1 == 0 && t2 != 0 {
                    return len1 + 1 == len2;
                }
                if t1 != 0 && t2 == 0 {
                    return len1 == len2 + 1;
                }
                if t1 != 0 && t2 != 0 {
                    return len1 == len2;
                }
            }
            if c1 == 1 && c2 > 1 {
                if t1 == 0 && t2 == 0 {
                    return len1 == len2 + 1;
                }
                if t1 == 0 && t2 != 0 {
                    return len1 == len2;
                }
                if t1 != 0 && t2 == 0 {
                    return len1 - 1 == len2 + 1;
                }
                if t1 != 0 && t2 != 0 {
                    return len1 - 1 == len2;
                }
            }
            if c1 > 1 && c2 == 1 {
                if t1 == 0 && t2 == 0 {
                    return len2 == len1 + 1;
                }
                if t1 != 0 && t2 == 0 {
                    return len2 == len1;
                }
                if t1 == 0 && t2 != 0 {
                    return len2 - 1 == len1 + 1;
                }
                if t1 != 0 && t2 != 0 {
                    return len2 - 1 == len1;
                }
            }
            if c1 > 1 && c2 > 1 {
                if t1 == 0 && t2 == 0 {
                    return len1 == len2;
                }
                if t1 != 0 && t2 == 0 {
                    return len1 == len2 + 1;
                }
                if t1 == 0 && t2 != 0 {
                    return len1 + 1 == len2;
                }
                if t1 != 0 && t2 != 0 {
                    return len1 == len2;
                }
            }
            false
        };
        for c1 in list.iter() {
            for c2 in list.iter() {
                if check(*c1, *c2) {
                    return true;
                }
            }
        }
        false
    }
}