LeetCode 3. Longest Substring Without Repeating Characters

주어진 문자열에서 문자가 반복되지 않는 가장 긴 서브(sub) 문자열 찾기


Description

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring,
"pwke" is a subsequence and not a substring.
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        
    }
}

Reference link (Click!)


Solution

/**
 * 3. Longest Substring Without Repeating Characters
 *
 * @author kimtaeng
 * created on 2018. 1. 9.
 */
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int length = 0;
        int[] arr = new int[128];

        for(int idx=0; idx<arr.length; idx++) {
            arr[idx] = 0;
        }

        int position = 0;
        int inputLength = s.length();
        for (int indexOfString = 0; indexOfString < inputLength; indexOfString++) {
            int charIndex = (int) s.charAt(indexOfString);
            position = getMax(arr[charIndex], position);
            length = getMax(length, indexOfString - position + 1);
            arr[charIndex] = indexOfString + 1;
        }
        return length;
    }

    public int getMax(int a, int b) {
        return a > b ? a : b;
    }
}

댓글을 남기시려면 Github 로그인을 해주세요 :D


Hi, there!

Thanks for visiting my blog.
Please let me know if there are any mistakes in my post.