isomorphic Strings
Original title link isomorphic Strings
Given two strings to determine if one can be converted to another, the conversion rule must be the same as a pair of characters two times the conversion must be the same, if the first character ' a ' is converted to ' B ', then the next time you encounter ' a ', it can only be converted to ' B ' cannot have two characters to the same character, If the first ' a ' is converted to ' B ', then no other characters can be converted to ' B '
Ideas:
Record conversion rules through two arrays until conversion is complete
The code is as follows
Class Solution {public
:
bool Isisomorphic (string s, String t) {
vector<int> nums (0);
Vector<int> count (0);
for (int i = 0; i < s.size (); ++i) {/
* has not been converted before, record conversion rule *
/if (nums[s[i]] = = 0 && Count[t[i]] = = 0) {
Nums[s[i]] = t[i];
Count[t[i]] = 1;
}
/* when nums[s[i]] = = T[i] is correct, otherwise returns false *
/else if (nums[s[i]! = T[i]) {
return false;
}
}
return true;
}
};
Word Pattern
Original topic link Word Pattern
Determines whether a single time in a string is arranged in a given pattern
Ideas:
is still a one-to-one relationship, and similar to the above
The code is as follows
Class Solution {public
:
bool Wordpattern (string pattern, string str) {
Unordered_map<char, string> hash;
Unordered_map<string, int> count;
Std::istringstream oss (str);
Std::string line;
for (auto Ch:pattern) {
std::getline (OSS, Line, ');
if (hash.count (ch) = = 0 && count.count (line) = = 0) {
hash[ch] = line;
Count[line] = 1;
}
else if (hash[ch]! = line) {
return false;
}
}
Return Std::getline (OSS, line)? false:true;
}
};