[Daily a small algorithm (seventy-six)] [String] with the longest substring of a character (thirty-seven mutual entertainment pen questions)

Introduction:
Today's entertainment to participate in Panax mutual written examination, the questions is one of a programming problem. Done before a longest covered substring of the title, but now this is not the same, here added a variant. If you want to see on a question, portal , you can look at, both to learn about.

Title Description

  • Now you string string, you find the code which does not contain a complete character and the longest sub repeated with a character string.
  • For example: "abcabcbb"
  • Output: 3
  • Explanation: Because the longest substring of characters without repetition is "abc", and with "a", so that a length of 3.
  • Enter: "bbbbb"
  • Output: 0
  • Explanation: Because the longest sub-string is repeated characters without "b", but not with "a", so that its length is 0.

Problem-solving ideas

In fact, this question is to find the longest substring of a variant of the question. I would like to address how to find the longest substring. Here we use HashMap to achieve. We use the HashMap key to save each character, use the value to save the current position +1. As used herein, to add value to one when there is repeated characters, skip to the next character in the same position directly. Then using two pointers, representing the left and right borders. If the right border duplication, the left margin to move to the next character position.
Now we have to find the longest substring method. If there is a character string a. This requires us to add a pointer, to do judgment, if the current character will be saved in the longest substring. This can be realized. Add the code below, accompanied by annotations should be able to understand.

Sample Code

package com.asong.leetcode.LongString;

import sun.security.krb5.SCDynamicStoreConfig;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 现在给你一串字符串,请你完成代码找出其中不含有重复字符并且带有a字符的最长子串。
 * 例如:“abcabcbb”
 * 输出:3
 * 解释:因为无重复字符的最长子串是“abc”,并且带有“a”,所以其长度为3.
 * 输入:“bbbbb”
 * 输出:0
 * 解释:因为无重复字符的最长子串是“b”,但其不带有“a”,所以其长度为0。
 *
 *
 **/
public class Solution {

    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        Solution solution = new Solution();
        while (scanner.hasNext())
        {
            String s = scanner.nextLine();
            int res = solution.lengthOfLongestSubstring(s);
            System.out.println(res);
        }
    }

    //滑动窗口来实现
    public int lengthOfLongestSubstring(String s)
    {
        int n = s.length();
        int ans = 0;
        Map<Character,Integer> map = new HashMap<>();
        int aAt = -1;
        //创建map窗口,i为左区间,j为右区间,右边界移动
        for (int j=0,i = 0; j < n; j++) {
            //判断子串中是否有字符a
            if(s.charAt(j)=='a')
            {
                aAt = j;
            }
            //如果重现重复字符,则将左边界移动到下一个字符
            if(map.containsKey(s.charAt(j)))
            {
                i = Math.max(map.get(s.charAt(j)),i);
            }
            //如果子串中有字符‘a’ 则找出最大子串
            if(aAt>=i)
            {
                ans = Math.max(ans,j-i+1);
            }
            // 将当前字符为key,下一个索引为value放入map中
            // value为j+1是为了当出现重复字符时,i直接跳到上个相同字符的下一个位置
            map.put(s.charAt(j),j+1);
        }
        return ans;
    }
}


He published 183 original articles · won praise 56 · views 5880

Guess you like

Origin blog.csdn.net/qq_39397165/article/details/104639280