1ms Java solution beats 97.91%


  • 0
    C
    public class Solution {
        public boolean wordPattern(String pattern, String str) {
            String[] splitArray = str.split(" ");
            if (pattern.length() != splitArray.length) return false;
            String[] bijections = new String[26];
            for (int i = 0; i < pattern.length(); i++) {
                int charPosition = pattern.charAt(i) - 'a';
                if (bijections[charPosition] != null && !bijections[charPosition].equals(splitArray[i])) return false;
                bijections[charPosition] = splitArray[i];
            }
            for (int i = 1; i < bijections.length; i++) {
                if (bijections[i] != null && bijections[i].equals(bijections[i - 1])) return false;
            }
            return true;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.