今天的字符類還比較簡單java
package y2019.Algorithm.str.easy; import java.util.HashMap; import java.util.Map; import java.util.Stack; /** * @ClassName IsValid * @Description 20. Valid Parentheses * * Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. * * An input string is valid if: * * Open brackets must be closed by the same type of brackets. * Open brackets must be closed in the correct order. * Note that an empty string is also considered valid. * * @Author xiaof * @Date 2019/8/4 15:46 * @Version 1.0 **/ public class IsValid { public boolean solution(String s) { Map comMap = new HashMap(); Stack stack = new Stack(); comMap.put('(', ')');comMap.put('[', ']');comMap.put('{', '}'); //1.遍歷字符串,獲取每個字符 char cs[] = s.toCharArray(); for (int i = 0; i < cs.length; ++i) { //2.判斷是是不是:(,[,{中的字符,若是是那麼就入棧,若是不是就出棧 if(comMap.containsKey(cs[i])) { //若是key包含 stack.push(cs[i]); } else { if(stack.size() <= 0) { return false; } //3.判斷出棧的數據和當前的數據是否正好配對,若是是,那麼就ok,若是不是,那麼就false char temp = (char) stack.pop(); if((char) comMap.get(temp) != cs[i]) { //若是不等 return false; } } } if(stack.size() > 0) { return false; } return true; } public static void main(String[] args) { String s = "()"; IsValid fuc = new IsValid(); fuc.solution(s); } }
package y2019.Algorithm.str.medium; /** * @ClassName CountSubstrings * @Description 647. Palindromic Substrings * * Given a string, your task is to count how many palindromic substrings in this string. * * The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters. * * Example 1: * * Input: "abc" * Output: 3 * Explanation: Three palindromic strings: "a", "b", "c". * * * Example 2: * * Input: "aaa" * Output: 6 * Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa". * * 統計字符中是否包含迴文字符的字串 * * @Author xiaof * @Date 2019/8/4 16:30 * @Version 1.0 **/ public class CountSubstrings { public int solution(String s) { //雙層循環遍歷全部字串 int count = 0; char[] source = s.toCharArray(); for (int i = 0; i < source.length; ++i) { for (int j = i; j < source.length; ++j) { //遍歷全部的字符串 if(isPalindromic(i, j, source)) { count++; } } } return count; } /** * * @param l 左邊索引 * @param r 右邊索引 * @param source 原始字符的字符數組 * @return */ public boolean isPalindromic(int l, int r, char[] source) { while (l <= r) { if (source[l] == source[r]) { ++l; --r; } else { return false; } } return true; } }
package y2019.Algorithm.str.medium; import java.util.HashMap; import java.util.Map; /** * @ClassName LengthOfLongestSubstring * @Description 3. Longest Substring Without Repeating Characters * * Given a string, find the length of the longest substring without repeating characters. * * Example 1: * * Input: "abcabcbb" * Output: 3 * Explanation: The answer is "abc", with the length of 3. * Example 2: * * Input: "bbbbb" * Output: 1 * Explanation: The answer is "b", with the length of 1. * Example 3: * * Input: "pwwkew" * Output: 3 * Explanation: 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. * * @Author xiaof * @Date 2019/8/4 17:30 * @Version 1.0 **/ public class LengthOfLongestSubstring { public int solution(String s) { if(s == null || s.equals("")) { return 0; } //統計最長連續子字串,那麼咱們只須要每次剔除重複的那個字符,而後從那個位置開始就能夠了 int start = 0, count = 1; Map num = new HashMap(); boolean lastcount = false; char[] source = s.toCharArray(); num.put(source[0], 0); for (int i = 1; i < source.length; ++i) { //判斷前面的字串中是否有包含,這裏要有(int) num.get(source[i]) >= start,並且是大於等於,避免以前跳過的數據干擾,而且不能排除掉起始位置 if (num.containsKey(source[i]) && (int) num.get(source[i]) >= start) { //若是包含了,說明以前已經出現重複的字串,那麼統計一波 count = Math.max(count, (i - start)); start = (int) num.get(source[i]) + 1; num.put(source[i], i); } else { num.put(source[i], i); if (i == source.length - 1) { lastcount = true; } } } //循環到最後,計算最後一個位置 if (lastcount) { count = Math.max(count, (source.length - start)); } return count; } public static void main(String[] args) { String s = "pwwkew"; String s1 = "tmmzuxt"; String s2 = "abcabcbb"; LengthOfLongestSubstring fuc = new LengthOfLongestSubstring(); fuc.solution(s2); } }