zoukankan      html  css  js  c++  java
  • WordPattern

    Given a pattern and a string str, find if str follows the same pattern.

    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:

    1. patterncontains only lowercase alphabetical letters, and str contains words separated by a single space. Each word in str contains only lowercase alphabetical letters.
    2. Both pattern and str do not have leading or trailing spaces.
    3. Each letter in pattern must map to a word with length that is at least 1.

    在LeeCode上有更简便的方法,使用了Map我对map还不够熟悉,所以在解决这个问题的时候,没有想起来用它

    代码:

    import java.util.Arrays;
    
    public class WordPattern {
    
    public static void main(String[] args) {
    String pattern = "abab";
    String str = "abc qwe abc qwe";
    boolean compare = wordPattern(pattern, str);
    if (compare) {
    System.out.println("true");
    } else {
    System.out.println("false");
    }
    }
    
    // 模式匹配
    public static boolean wordPattern(String pattern, String str) {
    // 转换为数组
    String[] strings = str.split(" ");
    char[] strings2 = pattern.toCharArray();
    
    // 判断长度
    if (strings2.length != strings.length) {
    return false;
    }
    
    // 数组记录替换
    int[] result1 = replaceString(strings);
    int[] result2 = replaceChar(strings2);
    Arrays.sort(result1);
    Arrays.sort(result2);
    if (!Arrays.equals(result1, result2)) {
    return false;
    }
    return true;
    }
    
    // 数组替换
    private static int[] replaceChar(char[] strings2) {
    int[] array = new int[strings2.length];
    for (int i = 0; i < array.length; i++) {
    array[i] = 0;
    }
    for (int i = 0; i < strings2.length; i++) {
    for (int j = 0; j < strings2.length; j++) {
    char temp = strings2[i];
    if (strings2[j] == temp) {
    if (array[j] == 0 && temp == strings2[j]) {
    array[j] = i + 1;
    }
    }
    }
    }
    return array;
    }
    
    // 数组替换
    private static int[] replaceString(String[] strings) {
    int[] array = new int[strings.length];
    for (int i = 0; i < array.length; i++) {
    array[i] = 0;
    }
    for (int i = 0; i < strings.length; i++) {
    String temp = strings[i];
    for (int j = 0; j < strings.length; j++) {
    if (temp.equals(strings[j]) && array[j] == 0) { // 此处注意 == 和
    // equals的区别
    array[j] = i + 1;
    }
    }
    }
    return array;
    }
    
    }
    
  • 相关阅读:
    Python环境搭建和开发工具的配置
    教材,教材,国内的教材你为何如此不堪入目?码农,码农,你到底该如何脱离码农?
    从一个新手容易混淆的例子简单分析C语言中函数调用过程
    E3: PS4/PC 莎木3 众筹200万美元 9小时内达成
    [原] blade中C++ singleton的实现
    [百度空间] [转] 四元数(Quaternions)
    [百度空间] [原]DLL导出实例化的模板类
    [百度空间] [转]DLL地狱及其解决方案
    [百度空间] [转+原]虚析构函数
    [百度空间] [转]程序员趣味读物:谈谈Unicode编码
  • 原文地址:https://www.cnblogs.com/tf-Y/p/4862758.html
Copyright © 2011-2022 走看看