天天看點

leetcode(一)Word Pattern

題目描述:

Given a 

pattern

 and a string 

str

, find if 

str

 follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in 

pattern

 and a non-empty word in 

str

.

Examples:

  1. pattern = 

    "abba"

    , str = 

    "dog cat cat dog"

     should return true.
  2. "abba"

    "dog cat cat fish"

     should return false.
  3. "aaaa"

    "dog cat cat dog"

  4. "abba"

    "dog dog dog dog"

本人思路:把字元串轉化為字元串數組後,先比較長度;再用pattern裡的字元替換掉字元串。全部替換後再重新轉化為字元串,并與pattern字元串相比較,得出結論。

代碼如下(方法略笨拙,可跳過看第二種,哈哈哈):

public bool WordPattern(string pattern, string str) {
            //char[] patternAttr=new char[pattern.Length]
            char[] patternAttr=pattern.ToCharArray();
            string[] strAttr=str.Split(' ');
            if(patternAttr.Length!=strAttr.Length)
            {
                return false;
            }
            else
            {
                for(int i=0;i<strAttr.Length;i++)
                {
                    if (patternAttr[i] != ' ')
                    {
                         for(int j=i+1;j<strAttr.Length;j++)
                        {
                   
                            if(strAttr[j]==strAttr[i])
                            {
                                strAttr[j]=patternAttr[i].ToString()+"!";
                                patternAttr[j] = ' ';
                            }
                        }
                         for (int k = i + 1; k < strAttr.Length;k++ )
                         {
                             if(patternAttr[k]==patternAttr[i])
                             {
                                 patternAttr[i] = ' ';
                             }
                         }
                         strAttr[i] = patternAttr[i].ToString()+"!";
                         patternAttr[i] = ' ';
                   }
                }
                str=String.Join("",strAttr);
                str=str.Replace("!","");
                if(str==pattern)return true;
                else return false;
          }
       }      

 第二種解題方法:用字典

public class Solution {
        public bool WordPattern(string pattern, string str) {
            string[] values = str.Split(' ');
            if(pattern.Length!=values.Length)
            {
                return false;
            }
            Dictionary<Char,String> dic=new Dictionary<Char,String>();
            for(int i=0;i<pattern.Length;i++)
            {
                if(!dic.ContainsKey(pattern[i]))
                {
                    if (!dic.ContainsValue(values[i]))
                    {
                        dic.Add(pattern[i], values[i]);
                    }
                    else return false;
                }
                else
                {
                    if(!dic[pattern[i]].Equals(values[i]))
                    {
                        return false;
                    }
                }
            }
            return true;
       }
}