leetcode上的pattern word问题

问题:

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.

解释:本问题要求做到一一对应,所以可以想到Java中的Map集合

问题还是比较简单的,关键就是细节方面,要把所有的问题都要考虑进去

代码:

import java.util.*;

public class Solution {

public boolean wordPattern(String pattern, String str) {

Map map = new HashMap();

char c;

String[] a = str.split(" ");

if(pattern.length() != a.length){

return false;

}

map.put(pattern.charAt(0),a[0]);

for(int i=1;i<a.length;i++){

c=pattern.charAt(i);

if(!map.containsKey(c) && !map.containsValue(a[i])){

map.put(c,a[i]);

}else if(!map.containsKey(c) && map.containsValue(a[i])){

return false;

}else{

String s = (String)map.get(c);

if(!s.equals(a[i])){

return false;

}

}

}

return true;

}

}

时间: 2024-12-23 12:04:46

leetcode上的pattern word问题的相关文章

leetcode 上的 bash 程序

*/--> pre.src {background-color: Black; color: White;} leetcode 上的 bash 程序 Table of Contents Tenth Line Transpose File Valid Phone Numbers Word Frequency 注: 以下程序大部份从 leetcode 的讨论上看来的... Tenth Line How would you print just the 10th line of a file? For

动态规划第五讲——leetcode上的题目动态规划汇总(上)

本节,我们将对leetcode上有关DP问题的题目做一个汇总和分析. 1.题目来源 Interleaving String 动态规划 二叉树 Unique Binary Search Trees 动态规划 二叉树 Word Break 动态规划 N/A Word Break II 动态规划 N/A Palindrome Partitioning 动态规划 N/A Palindrome Partitioning II 动态规划 N/A Triangle 动态规划 N/A Distinct Subs

LeetCode——Length of Last Word

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha

LeetCode: Length of Last Word [057]

昨天同事在做主从时,从库报如下错误: Got fatal error 1236 from master when reading data from binary log: 'Misconfigured master - server id was not set' 粗粗看好像是master的server-id没有设置,但同事做如下查询: 备库采集: [email protected] Fri May 23 14:18:59 2014 14:18:59 [(none)]> show variab

同一个程序eclipse上运行的结果与leetcode上运行的不一样

题目为leetcode第一道题Two Sum,以下为java写的代码: 当输入数据target=0,  nums=[0,4,3,0]时,eclipse上运行的结果与leetcode上运行的不同 1.eclipse下的运行结果: 2.leetcode下的运行结果: 把算法仔细理了一遍觉得并没有错 ,写的第一个leetcode卡在这了,好纠结!会不会是两者编译器差异造成的?以下贴出完整代码:

关于Leetcode上二叉树的算法总结

二叉树,结构很简单,只是比单链表复杂了那么一丢丢而已.我们先来看看它们结点上的差异: /* 单链表的结构 */ struct SingleList{ int element; struct SingleList *next; }; /* 二叉树的结构 */ struct BinaryTree{ int element; struct BinaryTree *left; struct BinaryTree *right; }; 根据以上两个结构,我们不难发现,单链表的结点只有一个指向下一结点的指针

关于leetcode上两数之和的思考

今天在leetcode上完成这道题目时: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1] 初步代码为 1 class Solution { 2 public int[] t

【leetcode?python】 290. Word Pattern

class Solution(object):    def wordPattern(self, pattern, str):        """        :type pattern: str        :type str: str        :rtype: bool        """        tag=0        tagdic={}        tagList=[]        i=0        while

[LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter