【Leetcode】Word Pattern

题目链接:https://leetcode.com/problems/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. 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.

思路:

1、pattern字符相等的位置对应的字符串中的单词应该是相等的,pattern字符不相等的位置对应的字符串中单词应该也是不相等的。时间复杂度为

O(n^2),空间复杂度为O(1)。

2、用HashMap存储字符到单词的映射关系,时间复杂度为O(n),空间复杂度为O(n)。

算法1:

[java] view
plain
 copy

  1. public boolean wordPattern(String pattern, String str) {
  2. String strs[] = str.split(" ");
  3. char patterns[] = pattern.toCharArray();
  4. if (strs.length != patterns.length || strs.length == 0) {
  5. return false;
  6. }
  7. for (int i = 0; i < patterns.length; i++) {
  8. for (int j = i + 1; j < patterns.length; j++) {
  9. if (patterns[i] == patterns[j]) { // 字符相等的时候,单词应该相等
  10. if (!strs[i].equals(strs[j])) {
  11. return false;
  12. }
  13. } else { // 字符不相等的时候,单词也应该不相等
  14. if (strs[i].equals(strs[j])) {
  15. return false;
  16. }
  17. }
  18. }
  19. }
  20. return true;
  21. }

算法2:

[java] view
plain
 copy

  1. public boolean wordPattern(String pattern, String str) {
  2. String strs[] = str.split(" ");
  3. char patterns[] = pattern.toCharArray();
  4. HashMap<Character,String> map = new HashMap<Character,String>();
  5. HashMap<String,Character> map2 = new HashMap<String,Character>();
  6. if (strs.length != patterns.length || strs.length == 0) {
  7. return false;
  8. }
  9. for (int i = 0; i < patterns.length; i++) {
  10. if(!map.containsKey(patterns[i])){
  11. map.put(patterns[i], strs[i]);
  12. }else{
  13. if(!map.get(patterns[i]).equals(strs[i])){
  14. return false;
  15. }
  16. }
  17. if(!map2.containsKey(strs[i])){
  18. map2.put(strs[i], patterns[i]);
  19. }else{
  20. if(!map2.get(strs[i]).equals(patterns[i])){
  21. return false;
  22. }
  23. }
  24. }
  25. return true;
  26. }
时间: 2024-12-05 15:43:28

【Leetcode】Word Pattern的相关文章

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【leetcode】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, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

【LeetCode】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】Word Break II 解题报告

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cats&quo

【LeetCode】Word Ladder 字符串

题目:Word Ladder <span style="font-size:18px;">/**LeetCode word ladder * 题目:给定一个起始单词和一个终结单词以及一个字典,要求每次变换一个字符,成为字典中新的词,直到变为最后的词,要求其最短路径 * 思路:利用队列,先弹出第一个词,分别将词中每一个字符替换直到找到一个字典中存在的词,加入队列,直到匹配的词是最后一个,此时终止 * 如果没有这样的路径,则返回0 */ package javaTrain; i

【leetcode】Word Break (middle)

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, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【LeetCode】Word Break II 动态规划

题目:Word Break 要求找到所有能够有字典中的词重组成目标串的结果 public class Solution { public static List<String> wordBreak(String s, Set<String> dict) { List<String> dp[] = new ArrayList[s.length()+1]; dp[0] = new ArrayList<String>(); for(int i=0; i<s.

【LeetCode】Word Break 动态规划

题目:Word Break 思路:将一个串可以划分的共有s.length+1个点,判断长为n的串是否能由字典中的词组成,则看之前有没有划分点能使其处于字典中 ,这样该问题 就分解为子问题的求解 所以可以使用动态规划 <span style="font-size:18px;">public class Solution { public boolean wordBreak(String s, Set<String> dict) { boolean[] tag =