LeetCode - Easy - 290. Word Pattern

淡淡的烟草味﹌ 2022-10-28 04:55 194阅读 0赞

Topic

  • Hash Table

Description

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

Given a pattern and a string s, find if s 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 s.

Example 1:

  1. Input: pattern = "abba", s = "dog cat cat dog"
  2. Output: true

Example 2:

  1. Input: pattern = "abba", s = "dog cat cat fish"
  2. Output: false

Example 3:

  1. Input: pattern = "aaaa", s = "dog cat cat dog"
  2. Output: false

Example 4:

  1. Input: pattern = "abba", s = "dog dog dog dog"
  2. Output: false

Constraints:

  • 1 <= pattern.length <= 300
  • pattern contains only lower-case English letters.
  • 1 <= s.length <= 3000
  • s contains only lower-case English letters and spaces ' '.
  • s does not contain any leading or trailing spaces.
  • All the words in s are separated by a single space.

Analysis

双向映射。

注意,Java包装整型的比较问题。

Submission

  1. import java.util.HashMap;
  2. import java.util.Objects;
  3. public class WordPattern {
  4. // 方法一:我写的,双向映射
  5. public boolean wordPattern1(String pattern, String s) {
  6. // 双向映射
  7. HashMap<Character, Integer> aMap = new HashMap<>();
  8. HashMap<String, Integer> bMap = new HashMap<>();
  9. String[] words = s.split(" ");
  10. if (pattern.length() != words.length)
  11. return false;
  12. int count = 0;// 这可省略,改用下面循环体的i变量
  13. for (int i = 0; i < pattern.length(); i++) {
  14. Character cr = pattern.charAt(i);
  15. Integer id1 = aMap.get(cr);
  16. Integer id2 = bMap.get(words[i]);
  17. if (id1 == null && id2 == null) {
  18. aMap.put(cr, count);
  19. bMap.put(words[i], count++);
  20. } else {
  21. if (id1 != id2)// 这要改成Objects.equals()
  22. return false;
  23. }
  24. }
  25. return true;
  26. }
  27. // 方法二:双向映射,比方法一更简洁
  28. public boolean wordPattern2(String pattern, String s) {
  29. String[] words = s.split(" ");
  30. if (words.length != pattern.length())
  31. return false;
  32. HashMap<Object, Object> index = new HashMap<>();//Object类型key即可放Character,亦可放String,我没想到
  33. for (Integer i = 0; i < words.length; ++i)
  34. if (!Objects.equals(index.put(pattern.charAt(i), i), //
  35. index.put(words[i], i)))
  36. return false;
  37. return true;
  38. }
  39. }

Test

  1. import static org.junit.Assert.*;
  2. import java.util.Objects;
  3. import org.junit.Test;
  4. public class WordPatternTest {
  5. @Test
  6. public void test() {
  7. WordPattern obj = new WordPattern();
  8. assertTrue(obj.wordPattern1("abba", "dog cat cat dog"));
  9. assertFalse(obj.wordPattern1("abba", "dog cat cat fish"));
  10. assertFalse(obj.wordPattern1("aaaa", "dog cat cat dog"));
  11. assertFalse(obj.wordPattern1("abba", "dog dog dog dog"));
  12. assertTrue(obj.wordPattern2("abba", "dog cat cat dog"));
  13. assertFalse(obj.wordPattern2("abba", "dog cat cat fish"));
  14. assertFalse(obj.wordPattern2("aaaa", "dog cat cat dog"));
  15. assertFalse(obj.wordPattern2("abba", "dog dog dog dog"));
  16. }
  17. @Test
  18. public void testEquals() {
  19. //== 与 !=运用在Integer包装类的比较的局限性
  20. //包装类最好用Objects.equals()
  21. int i = 127;
  22. Integer a = i;
  23. Integer b = i;
  24. assertTrue(a == b);
  25. assertTrue(Objects.equals(a, b));
  26. i = 128;
  27. a = i;
  28. b = i;
  29. assertFalse(a == b);
  30. assertTrue(Objects.equals(a, b));
  31. }
  32. }

发表评论

表情:
评论列表 (有 0 条评论,194人围观)

还没有评论,来说两句吧...

相关阅读

    相关 leetcode290

    LeetCode290----------Word Pattern 一个map是没办法搞定的,这是我一开始的思路,用一个map来保存char到string的映射,但是通过le