1657 a simple sorting problem
This commit is contained in:
parent
b3492b2e6a
commit
9bcda808e5
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
main
|
main
|
||||||
.vscode
|
.vscode
|
||||||
|
main.dSYM
|
50
1657-231130/main.cpp
Normal file
50
1657-231130/main.cpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include<stdcpp.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution{
|
||||||
|
public:
|
||||||
|
bool closeStrings(string word1, string word2){
|
||||||
|
if(word1.length()!=word2.length()) return false;
|
||||||
|
const int char_nums = 26;
|
||||||
|
int word1_chars[char_nums] = {};
|
||||||
|
int word2_chars[char_nums] = {};
|
||||||
|
for(int i = 0 ; i < word1.length();i++){
|
||||||
|
int word1_char = word1[i] - 'a';
|
||||||
|
int word2_char = word2[i] - 'a';
|
||||||
|
word1_chars[word1_char]++;
|
||||||
|
word2_chars[word2_char]++;
|
||||||
|
}
|
||||||
|
for(int i = 0 ; i <char_nums ; i++){
|
||||||
|
if((word1_chars[i]&&!word2_chars[i])||(!word1_chars[i]&&word2_chars[i]))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
sort(word1_chars,word1_chars + char_nums);
|
||||||
|
sort(word2_chars,word2_chars + char_nums);
|
||||||
|
for(int i = 0 ; i < char_nums ; i++){
|
||||||
|
if(word1_chars[i] != word2_chars[i])
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for(int i = 0 ; i < char_nums ; i++){
|
||||||
|
cout<<word1_chars[i]<<' '<<word2_chars[i]<<endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
Solution sol;
|
||||||
|
string word1 = "abc";
|
||||||
|
string word2 = "bca";
|
||||||
|
cout<<sol.closeStrings(word1,word2)<<endl;
|
||||||
|
word1 = "a";
|
||||||
|
word2 = "aa";
|
||||||
|
cout<<sol.closeStrings(word1,word2)<<endl;
|
||||||
|
word1 = "cabbba";
|
||||||
|
word2 = "abbccc";
|
||||||
|
cout<<sol.closeStrings(word1,word2)<<endl;
|
||||||
|
word1 = "uau";
|
||||||
|
word2 = "ssx";
|
||||||
|
cout<<sol.closeStrings(word1,word2)<<endl;
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user