获取任何给定输入字符串中具有不同字符的最长子字符串的长度

时间:2018-03-19 01:53:39

标签: java string algorithm

我已经实现了这个问题的逻辑,但它只对较小的字符串成功,并且时间限制超出了,并且对于较大的字符串输入,内存使用率非常高(如下所示)。我的意图是以与下面相同的方法实现该问题,但可能有所增强。有人能告诉我在哪里做错了吗?

输入:

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcde"

输出:

5

代码:

class Solution {
    public static int lengthOfLongestSubstring(String s) {

        int len=0;
        char[] cstr = null;
        LinkedHashSet<Character> lhs = new LinkedHashSet<Character>();
        HashMap<String,Integer> hm = new HashMap<String,Integer>();
        String newstr = "";

        //Generating all possible substrings using for loops as below and storing them in a hashmap. 
        for( int i=0;i<s.length();i++ )
        {
              for( int j=i+1;j<=s.length();j++ ) 
              {
                  //System.out.println(s.substring(i,j));
                  if( hm.containsKey( s.substring(i,j) ) )
                    hm.put( s.substring(i,j), hm.get( s.substring(i,j) ) + 1 );
                  else    
                    hm.put( s.substring(i,j), 1 ); 
              }  
        }  

        //Iterating through hashmap, get the distinct characters and store them in a linked hashset( for every iteration )              
        for( Map.Entry<String,Integer> m : hm.entrySet() ) {
            //System.out.println( m.getKey()+"-"+m.getValue() );
            cstr = m.getKey().toCharArray();

            //System.out.println(cstr);

            for( int i=0;i<cstr.length;i++ )
              lhs.add(cstr[i]);
            //System.out.println(lhs);

            for( Character c : lhs.toArray( new Character[lhs.size()]) )
              newstr += c.toString();
            //System.out.println(newstr);


            if( s.contains(newstr) && newstr.length() > len )
              len = newstr.length();

            cstr = null;
            lhs.clear();
            newstr = "";
        }  

        return len;
    }

  public static void main(String[] args) {

        System.out.println(lengthOfLongestSubstring("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcde"));

  }  

}

3 个答案:

答案 0 :(得分:2)

我将在python中提供一个解决方案,它具有时间复杂度(线性)。如果需要,您可以将其翻译为java。

def fun(s):
    last_index_of = [-1]*26
    last_index_of[ord(s[0])-ord('a')]=0
    length, max = 1,1
    for i in range(1,len(s)):
        index = ord(s[i])-ord('a')
        if last_index_of[index]==-1 or i-last_index_of[index]>length:
            length = length+1
            if length>max:
                max=length
        else:
            length = i - last_index_of[index]
        last_index_of[index]=i
return max

一些结果:

fun("abccab")
3
fun("abacc")
3
fun("ab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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcde")
5

答案 1 :(得分:0)

这是我使用普通索引并避免创建其他字符串的天真暴力解决方案。

public static String findLongestSubstringWithDistinctChars(String str)
{
    Objects.requireNonNull(str);

    int maxFrameBeginIndex = 0;
    int maxFrameLength = 0;

    for (int frameBeginIndex = 0; frameBeginIndex < str.length(); frameBeginIndex++)
    {
        for (int frameLength = 1; frameBeginIndex + frameLength <= str.length(); frameLength++)
        {
            // Check that last char in frame is not contained in the rest of the frame.
            char lastChar = str.charAt(frameBeginIndex + frameLength - 1);
            int frameEndIndexExclusive = frameBeginIndex + frameLength - 1;
            if (containsChar(str, frameBeginIndex, frameEndIndexExclusive, lastChar))
            {
                break;
            }

            if (frameLength > maxFrameLength)
            {
                maxFrameBeginIndex = frameBeginIndex;
                maxFrameLength = frameLength;
            }
        }
    }

    return str.substring(maxFrameBeginIndex, maxFrameBeginIndex + maxFrameLength);
}

/**
 * Checks whether str's substring of range [beginIndex,endIndexEclusive[
 * contains the character c.
 *
 * @param str
 *            the string
 * @param beginIndex
 *            the begin index
 * @param endIndexExclusive
 *            the end index, exclusive
 * @param c
 *            the character to check
 * @return whether c is contained in the substring
 */
private static boolean containsChar(String str, int beginIndex, int endIndexExclusive, char c)
{
    for (int i = beginIndex; i < endIndexExclusive; i++)
    {
        if (str.charAt(i) == c)
        {
            return true;
        }
    }
    return false;
}

你的样本输入在coderpad.io上需要大约1秒(有趣的基准测试^ _ ^)。

答案 2 :(得分:0)

我可能错过了OP中的一些内容,但是一个简单的解决方案可能只是在字符串本身上使用substringindexof等。我的解决方案如下:

public class LengthOfLongestSubstringWithUniqueCharacters {

    public static void main(String[] args) {

        String input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcde";

        int maxLength = -1;
        int startPos = -1;
        for (int i = 0; i < input.length(); i++) {
            int len = getSubstringWithUniqueCharacters(input, i);
            if (len > maxLength) {
                maxLength = len;
                startPos = i;
            }
        }
        System.out.println("String: " + input.substring(startPos, startPos + maxLength));
        System.out.println("Max: " + maxLength);

    }

    public static int getSubstringWithUniqueCharacters(String s, int pos) {
        int i = 0;
        for (; i < s.length() - pos; i++) {
            if (s.substring(pos, pos + i).indexOf(s.charAt(pos + i)) != -1) {
                return i;
            }
        }
        return i;
    }

}

/**
Output: 

String: abcde
Max: 5
*/