题目:

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.

思路:

  • 题意:给定一个字符串a,这个字符串的字符给定了一种模式,然后给定一个字符串b其中把字符串用空格隔开,判断b是否和a的格式一样,a里面的字符相同,b也要相同,a不同,b也不同,同时位置也一样
  • 采用的方法是a,转化为数组aa,bb,建立map,b根据空格,转化位数组,把a的存进map,判断map.containsKey(),包含去掉上一个重复值,添加新值,同时bb的相应位置元素也相同,不同的话,map.put,然后判断bb的元素和以前的都不同

    -注意长度相同,同时都为空时返回true

代码:

public class Solution {
    public boolean wordPattern(String pattern, String str) {
        Map<Character,Integer> pp = new HashMap<Character,Integer>();
        if(pattern == null && str == null){
            return true;
        }
        char[] p = pattern.toCharArray();
        String[] s = str.split(" ");
        if(p.length != s.length){
            return false;
        }else{
            for(int i = 0;i < p.length;i++){
                if(pp.containsKey(p[i])){
                    int k = pp.get(p[i]);
                    pp.remove(p[i]);
                    pp.put(p[i],i);
                    if(!s[k].equals(s[i])){
                        return false;
                    }
                }else{
                    pp.put(p[i],i);
                    for(int a = 0;a < i;a++){
                        if(s[a].equals(s[i])){
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
}

LeetCode(50)-Word Pattern的更多相关文章

  1. [LeetCode] 290. Word Pattern 单词模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

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

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

  3. [LeetCode] 290. Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  4. [LeetCode] 291. Word Pattern II 词语模式 II

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  5. leetcode 290. Word Pattern 、lintcode 829. Word Pattern II

    290. Word Pattern istringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,比较好的是,它不会将空格作为流,这样就实现了字符串的空格切割. C++引入了ost ...

  6. LeetCode 290. Word Pattern (词语模式)

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  7. LeetCode 290 Word Pattern

    Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

  8. leetcode(一)Word Pattern

    题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

  9. Leetcode 290 Word Pattern STL

    Leetcode 205 Isomorphic Strings的进阶版 这次是词组字符串和匹配字符串相比较是否一致 请使用map来完成模式统计 class Solution { public: boo ...

随机推荐

  1. Java实现内部类

    内部类是java中非常方便的一种机制,内部类所在的类称为宿主类,即内部类只能被它的宿主类使用,用这个特性,可以很好的控制类的可见性. 接下来看一个例子: package for_apro; impor ...

  2. 剑指offer面试题4 替换空格(c)

  3. 3.Lucene3.x API分析,Director 索引操作目录,Document,分词器

     1  Lucene卡发包结构分析 包名 功能 org.apache.lucene.analysis Analysis提供自带的各种Analyzer org.apache.lucene.colla ...

  4. JVM的GC(概念与深入)

    深入浅出了解什么是GC: http://my.oschina.net/xianggao/blog/86985 GC策略详解: http://blog.csdn.net/winniepu/article ...

  5. Java并发框架——AQS阻塞队列管理(二)——自旋锁优化

    看Craig, Landin, and Hagersten发明的CLH锁如何优化同步带来的花销,其核心思想是:通过一定手段将所有线程对某一共享变量轮询竞争转化为一个线程队列且队列中的线程各自轮询自己的 ...

  6. 安卓Eclipse如何快速修改工程名及包名

    修改工程名/包名,也是我们在开发中经常遇到的问题,比如我们在开发过程中突然觉得项目名不合适,或者根包名不合适想修改一下怎么办? 如上图所示工程,我想改变一下我的项目名,如把zzz改成xxx,把我的根包 ...

  7. 【java线程系列】java线程系列之java线程池详解

    一线程池的概念及为何需要线程池: 我们知道当我们自己创建一个线程时如果该线程执行完任务后就进入死亡状态,这样如果我们需要在次使用一个线程时得重新创建一个线程,但是线程的创建是要付出一定的代价的,如果在 ...

  8. UNIX网络编程——TCP服务器“拒绝服务攻击” 解决方案

    前面的博客<<使用select和shutdown>>里面的拒绝服务型攻击也有提到. 说这是一个完全的解决方案,其实有点夸大了,但这个方案确实可以缓解TCP服务器遭受" ...

  9. 初探linux子系统集之i2c子系统(二)

    大概也是前年了,一直没有把那个i2c的子系统讲解完,这里偷个懒,把以前整理的i2c相关的知识再梳理一下,做个了结,然后再去学习timer子系统. 先看下i2c在内核中的代码分布: obj-$(CONF ...

  10. 【一天一道LeetCode】#165. Compare Version Numbers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...