回分類題庫
d298: (1052cp2_ex3) Word Pattern
出處:LeetCode

Difficulity : 1
Accepted : 65 Times | Submit :204 Times | Clicks : 673
Accepted : 53 Users | Submit : 56 Users | Accepted rate : 95%
Time Limit :10000 ms | Memory Limit : 64000 KBytes
題目加入時間 : 2017-03-07 12:11

Content :

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. pattern = "abba", str = "dog cat cat fish" should return false.
  3. pattern = "aaaa", str = "dog cat cat dog" should return false.
  4. pattern = "abba", str = "dog dog dog dog" should return false.
You can start your code form here...
 
bool wordPattern(char* pattern, char* str) {
    
} 

Input :

 

  1. pattern contains only lowercase letters and a to z
  2. length of pattern: 1 ≦ N ≦ 100
  3. str contains lowercase letters separated by a single space.
  4. length of each word in str: 1 ≦ M ≦ 10
  5. length of pattern = number of word in str

 

Output :

Output True or False

Sample Input :

abba
dog cat cat dog
abba
dog cat cat fish
aaaa
dog cat cat dog
abba
dog dog dog dog 

Sample Output :

True
False
False
False

Hint :

https://en.wikipedia.org/wiki/Bijection
https://talknumber.blogspot.tw/2010/03/surjective-injective-and-bijective.html

Author :

LeetCode (管理員:cp2ta)

  Solve it!   Status Forum (0)

C++
C
JAVA
80126. nccucs105703030 (8 ms , 184KB)
80246. nccucs105703041 (2 ms , 184KB)
79956. nccucs105703009 (3 ms , 188KB)
79759. nccucs105703048 (3 ms , 192KB)
80184. nccucs102703020 (4 ms , 204KB)
79817. nccucs104703009 (4 ms , 189KB)
沒有解題記錄

執行時間會受很多因素影響因此僅供參考,主機等級請看這裡