290. 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 non-empty word in str.
class Solution {
public boolean wordPattern(String pattern, String str) {
char[] pat = pattern.toCharArray();
String[] arr = str.split(" ");
if(pat.length != arr.length) return false;
for(int j = 0 ; j < pat.length-1; j++) {
for(int i = j+1 ; i < pat.length ; i++) {
if((pat[i] == pat[j] && !arr[i].equals(arr[j])) || (pat[i] != pat[j] && arr[i].equals(arr[j]))) return false;
}
}
return true;
}
}
今天做了一道easy的题,却被一个知识点卡住了。
那就是判断String字符串是否相等。我一开始 判断 是通过 arr[i] == arr[j],这个是肯定不会相等的,因为这里其实比较的是地址值,而他们的地址值是肯定不相同的。
我又看了看String字符串的比较问题,java很多知识点都忘了呀!
java中String的相等判断(==和equals())详解
java中String相等的判断
标签:arr,pat,String,Pattern,length,str,pattern,Word,290 来源: https://blog.csdn.net/maomao_dreamhigh/article/details/89060104