Java Solution 17ms


  • 1
    F
    public class Solution {
        public int findSubstringInWraproundString(String p) {
            int[] map = new int[128];
            for(int i = 0; i < p.length(); i++){
                int j = i;
                while(i + 1 < p.length() && (p.charAt(i + 1) == p.charAt(i) + 1 || p.charAt(i + 1) + 25 == p.charAt(i))) i++;
                int len = i - j + 1;
                for(int k = j; k < j + Math.min(len, 26); k++){
                    map[p.charAt(k)] = Math.max(map[p.charAt(k)], i - k + 1);
                }
            }
            int res = 0;
            for(int e : map){
                res += e;
            }
    
            return res;
        }
    }
    

  • 0
    D

    @fatalme
    I had a similar logic as yours, however, I notice that if we replace 'i - k + 1' in the for loop with k - j + 1, the test case fails for the input -
    The only difference being that the i - k + 1 orders the numbers in a decreasing manner and k - j + 1 orders the numbers in increasing order.
    Any idea why would they differ in output?

    Input:
    "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
    Output:
    351
    Expected:
    33475


Log in to reply
 

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