Leetcode in Java && Oracle
424 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2023-01-02_03-24-42.png
61 KB
Runtime1 msBeats95.21%
Memory40.3 MBBeats73.73%

#N290. Word Pattern
problem link
#solution
class Solution {
public boolean wordPattern(String pattern, String s) {
Map<Character, String> map = new HashMap<>();
int i=0;
for(String part: s.split(" ")){
if(i==pattern.length()||map.containsKey(pattern.charAt(i)) && !map.get(pattern.charAt(i)).equals(part)
||map.containsValue(part) && get(map, part)!=pattern.charAt(i))
return false;

map.put(pattern.charAt(i), part);
i++;
}

return i==pattern.length();
}
public char get(Map<Character, String> map, String part){
for (Map.Entry<Character, String> entry : map.entrySet()) {
if (entry.getValue().equals(part)) {
return entry.getKey();
}
}
return '0';
}
}
👍2