Java DP Solution, easy understand


  • 1
    public class Solution {
        public int findSubstringInWraproundString(String p) {
            int[] alphabets=new int[26];
            if(p.isEmpty()) return 0;
             int len=1;
             alphabets[p.charAt(0)-'a']=1;
            for(int i=1;i<p.length();i++)
            {
                int current=p.charAt(i)-'a';
                int prev =p.charAt(i-1)-'a';
                if((prev+1)%26==current)
                 len++;
                else len=1;
                alphabets[current]=Math.max(alphabets[current],len);
            }
            int sum=0;
            for(int i:alphabets) sum+=i;
            return sum;
    
            
        }
    }
    

Log in to reply
 

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