- 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:
- pattern = “abba”, str = “dog cat cat dog” should return true.
- pattern = “abba”, str = “dog cat cat fish” should return false.
- pattern = “aaaa”, str = “dog cat cat dog” should return false.
- 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.
https://discuss.leetcode.com/topic/26376/short-c-read-words-on-the-fly
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.
1 | bool wordPattern(string pattern, string str) { |
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:
1 | def wordPattern(self, pattern, str): |
Improved version also from there:
1 | def wordPattern(self, pattern, str): |
From here:
1 | def wordPattern(self, pattern, str): |
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
1 | bool wordPattern(string pattern, string str) { |
https://discuss.leetcode.com/topic/36612/my-solution-in-python
My solution in python
1 | class Solution(object): |
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
1 | public class Solution { |