题目内容
塔子哥是一名程序员,他正在参加百度公司的笔试面试。在面试中,他得到了一个有趣的编程问题,需要判断给定字符串能否通过重新排列组成 Baidu
这个单词。
他知道这个问题在实际开发中非常有用,因为它可以帮助检查用户输入是否符合要求。于是,他开始思考如何解决这个问题,并开始动手编写代码,现在塔子哥拿到了一个字符串,他想知道这个字符串能否通过重新排列组成 Baidu
字符串?
注:必须大小写完全相同。
共有 t t t 组询问。
输入描述
第一行输入一个正整数 t t t ,代表询问次数。
接下来的 t t t 行,每行输入一个仅包含英文字母的字符串。
所有字符串的长度之和保证不超过 200000 200000 200000 。
输出描述
对于每次询问,输出一行答案。如果可以通过重新排列组成 Baidu
,则输出 Yes
,否则输出 No
。
样例
输入
3
aiBdu
Bduai
bdaui
输出
Yes
Yes
No
题目思路
对字符串排序,然后判断是否与"Badiu"相等即可。
代码
C++
#include<bits/stdc++.h>
using namespace std;
int main (){
int t;
cin >> t;
string a = "Baidu";
sort(a.begin() , a.end());
while (t--){
string x;
cin >> x;
sort(x.begin() , x.end());
if (x == a) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
python
n = int(input())
a = list("Baidu")
a.sort()
a = "".join(a)
for i in range (n):
x = list(input())
x.sort()
x = "".join(x)
if a == x:
print("Yes")
else:
print("No")