• 32.5%

https://leetcode.com/problems/word-pattern/#/description

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.

Notes:

You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.

Short C++, read words on the fly

I think all previous C++ solutions read all words into a vector at the start. Here I read them on the fly.

https://discuss.leetcode.com/topic/26316/short-in-python

Short in Python

This problem is pretty much equivalent to Isomorphic Strings. Let me reuse two old solutions.

From here:

Improved version also from there:

From here:

Thanks to zhang38 for pointing out the need to check len(s) == len(t) here.

https://discuss.leetcode.com/topic/26313/0ms-c-solution-using-istringstream-and-double-maps

0ms C++ solution using istringstream and double maps

https://discuss.leetcode.com/topic/36612/my-solution-in-python

My solution in python

please point out if there’s anything i should improve

2ms, 51.78%, October 18, 2016

https://discuss.leetcode.com/topic/26339/8-lines-simple-java