Google Interview Question
SDE1sCountry: United States
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
bool IsEncoded(string const &s, string const &pattern)
{
if (s.size() != pattern.size()) {
return false;
}
unordered_map<char, char> seen_in_s, seen_in_p;
for (int i = 0; i < s.size(); ++i) {
auto it = seen_in_s.find(s[i]);
if (it != seen_in_s.end()) {
if (pattern[i] != it->second) {
return false;
}
} else {
seen_in_s[s[i]] = pattern[i];
}
it = seen_in_p.find(pattern[i]);
if (it != seen_in_p.end()) {
if (s[i] != it->second) {
return false;
}
} else {
seen_in_p[pattern[i]] = s[i];
}
}
return true;
}
class EncodingChecker {
public:
EncodingChecker(vector<string> const &patterns)
{
for (auto &p : patterns) {
normalized_patterns_.insert(Normalize(p));
}
}
bool IsEncoded(string const &s) const
{
return normalized_patterns_.find(Normalize(s)) != normalized_patterns_.end();
}
private:
string Normalize(string const &s) const
{
unordered_map<char, char> seen;
char val = 0;
string out;
for (char c : s) {
auto it = seen.find(c);
if (it != seen.end()) {
out += it->second;
} else {
out += val;
seen[c] = val++;
}
}
return out;
}
unordered_set<string> normalized_patterns_;
};
int main()
{
cout << IsEncoded("abcdef", "abcabc") << "\n";
cout << IsEncoded("cbzabc", "abcabc") << "\n";
cout << IsEncoded("xyzxyz", "abcabc") << "\n";
EncodingChecker checker({"abcabc", "yzyzyz", "aaabbbaaa"});
cout << checker.IsEncoded("123123") << "\n";
cout << checker.IsEncoded("hahaha") << "\n";
cout << checker.IsEncoded("777999777") << "\n";
cout << checker.IsEncoded("121213") << "\n";
}
@Shah. Thank you! Fixed the first part.
Regarding the second part. I assume that the pattern doesn't force distance between characters. I.e. I assume that pattern "aza" matches string "121", doesn't taking in account that distance from 'a' to 'z' is not the same as distance from '1' to '2'. If there was an interviewer, it's definitely a good question to ask if the pattern should force distance or not.
Check if following works -
class EncodingChecker {
String patternString=null;
EncodingChecker (String pattern) {
patternString = getpattern(pattern);
}
boolean isEncoded (String s) {
return patternString.equals(getpattern(s));
}
String getpattern(String str){
String[] arr = str.toChar();
Map<String,int> map = new HashMap<String,int>();
StringBuilder pattern = new StringBuilder(str.length);
int index =1;
for(String s:arr){
if(map.containsKey(s)){
pattern.append(map.get(s));
}else{
map.put(s,index);
pattern.append(index++);
}
}
}
}
class EncodingChecker {
String patternString=null;
EncodingChecker (String pattern) {
patternString = getpattern(pattern);
}
boolean isEncoded (String s) {
return patternString.equals(getpattern(s));
}
String getpattern(String str){
String[] arr = str.toChar();
Map<String,int> map = new HashMap<String,int>();
StringBuilder pattern = new StringBuilder(str.length);
int index =1;
for(String s:arr){
if(map.containsKey(s)){
pattern.append(map.get(s));
}else{
map.put(s,index);
pattern.append(index++);
}
}
}
}
@alex
- Shah December 20, 2017Your first method fails since all it does it checks to see if the character is unique in the string. It doesn't take into account or the character in the pattern is being repeated.
Ex:
S= abcdef
Pattern = abcabc
Your function will return true.
Also, your second method also fails since all it does is it captures the unique character count. It doesn't check if it matches the pattern. For ex:
Pattern= abcdef
S= kaulpj
The pattern is that each char is one off the previous where as the string doesn't capture that. This will pass since all chars are unique in both pattern and s. Is this relevant in for this question? Not sure :0