C# Solution


  • 0
        public bool WordPattern(string pattern, string str) {
            Hashtable hashPattern = new Hashtable(),
                      hashStr = new Hashtable();
            string[] splitedString = str.Split(' ');
            
            if (pattern.Length != splitedString.Length)
                return false;
            
            for (int i = 0; i <= pattern.Length - 1; i++)
            {
                if ((hashPattern.Contains(pattern[i]) && (string)hashPattern[pattern[i]] != splitedString[i]) ||
                    (hashStr.Contains(splitedString[i]) && (char)hashStr[splitedString[i]] != pattern[i]))
                    return false;
                
                if (!hashPattern.Contains(pattern[i]))
                    hashPattern.Add(pattern[i], splitedString[i]);
                
                if (!hashStr.Contains(splitedString[i]))
                    hashStr.Add(splitedString[i], pattern[i]);
            }
            
            return true;
        }

Log in to reply
 

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