Explain testcase "ab" "happy hacking"


  • 0
    L

    0_1509037172269_0c49ce74-866a-4e1f-85d1-10f906c54af3-image.png


  • 0
    L

    my code:

    bool wordPattern(string pattern, string str) {
    	if (pattern.length() == 0 || str.length() == 0)
    		return false;
    	string temp;
    	for (int i = 0; i < str.length(); i++)
    	{
    		if (str[i] != ' ')
    		{
    			temp.push_back(str[i]);
    			i++;
    		}
    		while (str[i] != ' '&&i < str.length())
    		{
    			i++;
    		}
    	}
    	if (temp.length() != pattern.length())
    		return false;
    	int ttt1[26] = { 0 }, ttt2[26] = { 0 };
    	for (int i = 0; i<pattern.length(); i++)
    	{
    		if (ttt1[temp[i] - 'a'] == 0 && ttt2[pattern[i] - 'a'] == 0)
    		{
    			ttt1[temp[i] - 'a'] = pattern[i];
    			ttt2[pattern[i] - 'a'] = temp[i];
    		}
    		else
    			if (ttt1[temp[i] - 'a'] != pattern[i] || ttt2[pattern[i] - 'a'] != temp[i])
    				return false;
    	}
    	return true;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.