Simple Java Solution with List


  • 0
    R
    public class Solution {
        public int lengthOfLongestSubstring(String s) {
            int result = 0;
            String temp ="";
            List<Character> list = new ArrayList();
            char[] sArray = s.toCharArray();
            int startIndex = 0,stopIndex =0;
            for(int i=0; i<sArray.length; i++){
                if(list.contains(sArray[i])){
                    stopIndex =list.indexOf(sArray[i]);
                    if(list.size() > result){
                        result =list.size();
                    }
                    while(stopIndex>=0){
                        list.remove(0);
                        stopIndex--;
                    }
                    list.add(sArray[i]);
                }
                else
                list.add(sArray[i]);
            }
            if(list.size() > result){
                        result =list.size();
                    }
            return result;
        }
    }
    

Log in to reply
 

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