NC221034. swapstring
描述
输入描述
The first line is the first string S1, and the second line is the second string S2
输出描述
If you can make two strings equal by exchanging, output yeS, otherwise output N0
示例1
输入:
aab aba
输出:
yeS
说明:
Just swap 'a' and 'b'
示例2
输入:
abcd dcba
输出:
N0
说明:
Can't do itJava 解法, 执行用时: 31ms, 内存消耗: 12244K, 提交时间: 2021-05-28 19:50:29
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1 = sc.next(); String s2 = sc.next(); char[] a1 = s1.toCharArray(); char[] a2 = s2.toCharArray(); int len = a1.length; int count = 0; for(int i = 0; i< len ; i++){ if( a1[i] != a2[i])count++; } if(count <= 2) System.out.println("yeS"); else System.out.println("N0"); } }
C(clang11) 解法, 执行用时: 2ms, 内存消耗: 368K, 提交时间: 2021-04-20 13:40:26
#include <stdio.h> int main() { char s1[1005],s2[1005]; scanf("%s%s",s1,s2); int sum = 0; for (int i = 0;i < strlen(s1);i ++){ if (s1[i] != s2[i]){ sum ++; } } if (sum == 2 || sum == 0){ printf("yeS"); } else { printf("N0"); } return 0; }
C++ 解法, 执行用时: 6ms, 内存消耗: 380K, 提交时间: 2021-05-28 17:10:47
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int main(){ string a,b; cin>>a>>b; int x=0; for(int i=0;i<a.length() ;i++){ if(a[i]!=b[i]) x++; } if(x>2) cout<<"N0"; else cout<<"yeS"; return 0; }
Python3(3.9) 解法, 执行用时: 33ms, 内存消耗: 6884K, 提交时间: 2021-04-20 15:45:16
s1 =list(input()) s2 =list(input()) num = 0 for i in range(len(s1)): if s1[i]!=s2[i]: num += 1 if num==0 or num==2: print('yeS') else : print('N0')