LeetCode_290. Word Pattern

290. Word Pattern

Easy

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.

Example 1:

Input: pattern = "abba", str = "dog cat cat dog"
Output: true

Example 2:

Input:pattern = "abba", str = "dog cat cat fish"
Output: false

Example 3:

Input: pattern = "aaaa", str = "dog cat cat dog"
Output: false

Example 4:

Input: pattern = "abba", str = "dog dog dog dog"
Output: false

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

package leetcode.easy;

public class WordPattern {
	@org.junit.Test
	public void test() {
		String pattern1 = "abba";
		String str1 = "dog cat cat dog";
		String pattern2 = "abba";
		String str2 = "dog cat cat fish";
		String pattern3 = "aaaa";
		String str3 = "dog cat cat dog";
		String pattern4 = "abba";
		String str4 = "dog dog dog dog";
		System.out.println(wordPattern(pattern1, str1));
		System.out.println(wordPattern(pattern2, str2));
		System.out.println(wordPattern(pattern3, str3));
		System.out.println(wordPattern(pattern4, str4));
	}

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

		java.util.Map<String, Character> map = new java.util.HashMap<>();
		for (int i = 0; i < pattern.length(); i++) {
			if (map.containsKey(arrays[i]) && map.get(arrays[i]) != pattern.charAt(i)) {
				return false;
			} else if (!map.containsKey(arrays[i]) && map.containsValue(pattern.charAt(i))) {
				return false;
			} else {
				map.put(arrays[i], pattern.charAt(i));
			}
		}
		return true;
	}
}

原文地址:https://www.cnblogs.com/denggelin/p/11777309.html

时间: 2024-11-05 11:32:10

LeetCode_290. Word Pattern的相关文章

LeetCode:Word Pattern - 字符串模式匹配

1.题目名称 Word Pattern(字符串模式匹配) 2.题目地址 https://leetcode.com/problems/word-pattern/ 3.题目内容 英文:Given a pattern and a string str, find if str follows the same pattern. 中文:给出一组模式(pattern)和一个字符串(str),查看字符串是否与模式匹配 例如: pattern = "abba",str = "dog cat

[LeetCode][JavaScript]Word Pattern

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 substring in str. Examples: pattern = "abba", str = "dog c

LeetCode Word Pattern II

原题链接在这里:https://leetcode.com/problems/word-pattern-ii/ 题目: 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 substring in

290. Word Pattern

/* * 290. Word Pattern * 2016-7-2 by Mingyang * 这里加上了没有containsValue,因为这里如果abba 和 dog dog dog dog通不过, * 因为a已经包含dog了,b却也包含了dog,解决方法就是value不能重复 * 直接用containsValue就好了 */ public static boolean wordPattern(String pattern, String str) { String[] array=str.

Word Pattern

?package cn.edu.xidian.sselab.hashtable; import java.util.HashMap;import java.util.Map; /** *  * @author zhiyong wang * title: Word Pattern * content: * Given a pattern and a string str, find if str follows the same pattern. * Here follow means a ful

Word Pattern II 解答

Question 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 substring in str. Examples: pattern = "abab", str = "

Word Pattern | &amp; II

Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "abba", str = "dog cat cat dog" should return true. pattern = "abba", str = "dog cat cat fish" should return

291.Word Pattern II

/* * 291.Word Pattern II * 2016-3-13 by Mingyang * 这道题目有一定的难度,又是一个backtracking boolean的题目,不过这里多了一个参数Hashmap * 1.长度标准:无 * 2.可选的范围:从j开始,也就是从str未match的第一个字符开始的一系列的substring * 3.往前走一步:改变map,将两个对应的东西分别放入map * 4.后退一步:把map刚刚加入的值去掉 * 5.特别的case:刚好搜完和一个搜完了另一个没

LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pattern = "abba". str = "dog cat cat dog" 应该返回真. pattern = "abba", str = "dog cat cat fish" 应该返回假. pattern = "aaa