Skip to content

Files

Latest commit

f385924 · Apr 29, 2022

History

History
178 lines (142 loc) · 4.42 KB

File metadata and controls

178 lines (142 loc) · 4.42 KB

English Version

题目描述

给你长度相等的两个字符串 s1s2 。一次 字符串交换 操作的步骤如下:选出某个字符串中的两个下标(不必不同),并交换这两个下标所对应的字符。

如果对 其中一个字符串 执行 最多一次字符串交换 就可以使两个字符串相等,返回 true ;否则,返回 false

 

示例 1:

输入:s1 = "bank", s2 = "kanb"
输出:true
解释:例如,交换 s2 中的第一个和最后一个字符可以得到 "bank"

示例 2:

输入:s1 = "attack", s2 = "defend"
输出:false
解释:一次字符串交换无法使两个字符串相等

示例 3:

输入:s1 = "kelb", s2 = "kelb"
输出:true
解释:两个字符串已经相等,所以不需要进行字符串交换

示例 4:

输入:s1 = "abcd", s2 = "dcba"
输出:false

 

提示:

  • 1 <= s1.length, s2.length <= 100
  • s1.length == s2.length
  • s1s2 仅由小写英文字母组成

解法

Python3

class Solution:
    def areAlmostEqual(self, s1: str, s2: str) -> bool:
        cnt, n = 0, len(s1)
        c1 = c2 = None
        for i in range(n):
            if s1[i] != s2[i]:
                cnt += 1
                if (cnt == 2 and (s1[i] != c2 or s2[i] != c1)) or cnt > 2:
                    return False
                c1, c2 = s1[i], s2[i]
        return cnt == 0 or cnt == 2

Java

class Solution {
    public boolean areAlmostEqual(String s1, String s2) {
        int n = s1.length();
        int cnt = 0;
        char c1 = 0;
        char c2 = 0;
        for (int i = 0; i < n; ++i) {
            char t1 = s1.charAt(i), t2 = s2.charAt(i);
            if (t1 != t2) {
                ++cnt;
                if ((cnt == 2 && (c1 != t2 || c2 != t1)) || cnt > 2) {
                    return false;
                }
                c1 = t1;
                c2 = t2;
            }
        }
        return cnt == 0 || cnt == 2;
    }
}

C++

class Solution {
public:
    bool areAlmostEqual(string s1, string s2) {
        char c1 = 0, c2 = 0;
        int n = s1.size();
        int cnt = 0;
        for (int i = 0; i < n; ++i)
        {
            if (s1[i] != s2[i])
            {
                ++cnt;
                if ((cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2) return false;
                c1 = s1[i];
                c2 = s2[i];
            }
        }
        return cnt == 0 || cnt == 2;
    }
};

Go

func areAlmostEqual(s1 string, s2 string) bool {
	var c1, c2 byte
	cnt, n := 0, len(s1)
	for i := 0; i < n; i++ {
		if s1[i] != s2[i] {
			cnt++
			if (cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2 {
				return false
			}
			c1, c2 = s1[i], s2[i]
		}
	}
	return cnt == 0 || cnt == 2
}

Rust

impl Solution {
    pub fn are_almost_equal(s1: String, s2: String) -> bool {
        let (s1, s2) = (s1.as_bytes(), s2.as_bytes());
        let n = s1.len();
        let mut indexs = vec![];
        for i in 0..n {
            let (c1, c2) = (s1[i], s2[i]);
            if c1 != c2 {
                indexs.push(i);
                if indexs.len() > 2 {
                    return false;
                }
            }
        }
        let size = indexs.len();
        if size == 2 {
            return s1[indexs[0]] == s2[indexs[1]] && s2[indexs[0]] == s1[indexs[1]];
        }
        size != 1
    }
}

...