Home > Back-end >  For bosses to help me explain this code
For bosses to help me explain this code

Time:09-19

Topic: word rule
Given a regular pattern and a string STR, judge whether the STR follow the same rule,

In this case, the following match exactly, for example, each of the letters in the pattern and the each non-empty string STR word corresponding laws, there is a two-way connection between

Example 1:

Input: the pattern="abba", STR="dog cat cat dog
"Output: true
Example 2:

Input: the pattern="abba", STR="dog cat cat fish"
Output: false
Example 3:

Input: the pattern="aaaa", STR="dog cat cat dog
"Output: false
Example 4:

Input: the pattern="abba", STR="dog dog dog dog"
Output: false
Description:
You can assume that the pattern will contain only lowercase letters, STR contains lowercase characters by a single blank space,

Bool wordPattern (char * pattern, char * STR) {
Char * * hash=(char * *) malloc (26 * sizeof (char *));
for (int i=0; i <26. + + I) {
Hash [I]=(char *) malloc (64 * sizeof (char));// here?
Memset (hash [I], 0, 64 * sizeof (char));
}
Int len=strlen (pattern);
for (int i=0; i can't understand all the
Char * p=STR.
While (p & amp; & * p!=0 & amp; & * p!=' ') + + p;// the pointer p with zero here? How can use zero?
If ("==* p) * p++=0;// * p++=0 is what mean? In relation to how Pointers and zero?
If (strlen (STR)==0)
return false;
Int pos=pattern [I] - 'a';
If (strlen (hash (pos))==0) {// down here don't understand the
For (int j=0; J & lt; 26. + + j) {
If (j!=pos & amp; & [j] strlen (hash) & gt; 0 {
If (STRCMP (hash [j], STR)==0)// string STR is not greater than the length of the string hash [j]? How could the STRCMP () function result is equal to zero?
return false;
}
}
Strcpy (hash (pos), STR);
}
The else {
If (STRCMP (hash (pos), STR)!=0)
return false;
}
STR=p;
}
If (strlen (STR) & gt; 0)
return false;
return true;
}
to summarise the overall train of thought
  • Related