Skip to content

Instantly share code, notes, and snippets.

@shixiaoyu
Created June 25, 2019 16:53
Show Gist options
  • Save shixiaoyu/31093b21853ec3f06cb859e7556e96db to your computer and use it in GitHub Desktop.
Save shixiaoyu/31093b21853ec3f06cb859e7556e96db to your computer and use it in GitHub Desktop.
// Reference: https://leetcode.com/problems/word-pattern/discuss/73402/8-lines-simple-Java
public boolean wordPattern(String pattern, String str) {
String[] words = str.split(" ");
if (words.length != pattern.length())
return false;
Map index = new HashMap();
for (Integer i=0; i<words.length; ++i)
if (index.put(pattern.charAt(i), i) != index.put(words[i], i))
return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment