[LeetCode]--290. 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:
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.

Credits:
Special thanks to @minglotus6 for adding this problem and creating all test cases.

我开始以为模式都是aabb这种,就是都是a开始这种。不会出现eebb这样的,后来基于这种错误的改造,得到以下方法。

public boolean wordPattern(String pattern, String str) {
        String[] strs = str.split(" ");
        if (pattern.length() != strs.length)
            return false;
        Map<String, Character> map = new HashMap<String, Character>();
        String temp = "";
        char c = 'a';
        for (int i = 0; i < strs.length; i++) {
            if (!map.containsKey(strs[i]))
                map.put(strs[i], c++);
            temp = temp + map.get(strs[i]);
        }
        return temp.equals(transformPattern(pattern));
    }

    public String transformPattern(String s) {
        char c = 'a';
        String temp = "";
        char[] chs = s.toCharArray();
        Map<Character, Character> map = new HashMap<Character, Character>();
        for (int i = 0; i < s.length(); i++) {
            if (!map.containsKey(chs[i]))
                map.put(chs[i], c++);
            temp = temp + map.get(chs[i]);
        }
        return temp;
    }

如果再弄个set的话,一次遍历就能完成。

public boolean wordPattern(String pattern, String str) {
        String[] strs = str.split(" ");
        if (pattern.length() != strs.length)
            return false;

        Map<Character, String> map = new HashMap<Character, String>();
        Set<String> unique = new HashSet<String>();

        for (int i = 0; i < pattern.length(); i++) {
            char c = pattern.charAt(i);
            if (map.containsKey(c)) {
                if (!map.get(c).equals(strs[i]))
                    return false;
            } else {
                if (unique.contains(strs[i]))
                    return false;
                map.put(c, strs[i]);
                unique.add(strs[i]);
            }
        }
        return true;
    }
时间: 2024-09-10 03:58:45

[LeetCode]--290. Word Pattern的相关文章

[LeetCode] Longest Word in Dictionary 字典中的最长单词

Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest l

[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: pattern = "abba", str = "dog cat cat d

[LeetCode]139.Word Break

题目 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", dict = ["leet", "code"]. Return true beca

[LeetCode]127.Word Ladder

题目 Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: s

[LeetCode]79.Word Search

[题目] Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

Word Search -- LeetCode

原题链接: http://oj.leetcode.com/problems/word-search/ 这道题很容易感觉出来是图的题目,其实本质上还是做深度优先搜索.基本思路就是从某一个元素出发,往上下左右深度搜索是否有相等于word的字符串.这里注意每次从一个元素出发时要重置访问标记(也就是说虽然单次搜索字符不能重复使用,但是每次从一个新的元素出发,字符还是重新可以用的).深度优先搜索的算法就不再重复解释了,不了解的朋友可以看看wiki - 深度优先搜索.我们知道一次搜索的复杂度是O(E+V),

SqlParser 一个利用正则表达式解析单句SQL的类_正则表达式

先看要解析的样例SQL语句: 复制代码 代码如下: select * from dual SELECT * frOm dual Select C1,c2 From tb select c1,c2 from tb select count(*) from t1 select c1,c2,c3 from t1 where condi1=1 Select c1,c2,c3 From t1 Where condi1=1 select c1,c2,c3 from t1,t2 where condi3=3

WordPress屏蔽非法关键词用户名的方法

下面就给大家分享一个WordPress网站注册用户屏蔽非法关键词用户名的方法. 首先在functions.php文件中加入以下两段代码:  代码如下 复制代码 function uedsc_user_blacklist_check($str){     $moderation_keys = trim(get_option('moderation_keys'));     $blacklist_keys = trim(get_option('blacklist_keys'));         $