1
2

yukicoder No.69 文字を自由に並び替え 解説

Posted at

問題文

解説

まず、同じ文字を組み合わせてできた別の単語があったとする。これらは$A$と比較した結果が同じになるだろう。順番は気にしてないのだから。
つまり、$B$は自分の都合のいいように並び替えても問題がないといえる。
では、今回の場合どうすれば都合がよくなるか、それは昇順ソートである。
$A$と$B$を事前に昇順ソートしてしまえば後は$A$と$B$が一致するかという問題に帰着する。

C++での解答例

#include <bits/stdc++.h>
using namespace std;

int main(){
  string a,b;cin>>a>>b;
  sort(a.begin(),a.end());
  sort(b.begin(),b.end());
  cout<<(a==b?"YES\n":"NO\n");
}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2