视频教程:B站、网易云课堂、腾讯课堂
代码地址:Gitee、Github
存储地址:
百度云-提取码:
Google云

  1. 字符串中的第一个唯一字符

国内站

java版

/*
思路:

*/




/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

国外站

java版

/*
思路:

*/

public class Solution {
    public int firstUniqChar(String s) {
        int freq [] = new int[26];
        for(int i = 0; i < s.length(); i ++)
            freq [s.charAt(i) - 'a'] ++;
        for(int i = 0; i < s.length(); i ++)
            if(freq [s.charAt(i) - 'a'] == 1)
                return i;
        return -1;
    }
}


/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

  1. 字符串转换整数 (atoi)

国内站

java版

/*
思路:

*/




/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

国外站

java版

/*
思路:

*/


class Solution {
	public static int myAtoi(String str) {
	if (str == null || str.length() == 0)
		return 0;//
	str = str.trim();
	char firstChar = str.charAt(0);
	int sign = 1, start = 0, len = str.length();
	long sum = 0;
	if (firstChar == '+') {
		sign = 1;
		start++;
	} else if (firstChar == '-') {
		sign = -1;
		start++;
	}
	for (int i = start; i < len; i++) {
		if (!Character.isDigit(str.charAt(i)))
			return (int) sum * sign;
		sum = sum * 10 + str.charAt(i) - '0';
		if (sign == 1 && sum > Integer.MAX_VALUE)
			return Integer.MAX_VALUE;
		if (sign == -1 && (-1) * sum < Integer.MIN_VALUE)
			return Integer.MIN_VALUE;
	}

	return (int) sum * sign;
}
}

/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

  1. 反转字符串 II

国内站

java版

/*
思路:

*/




/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

国外站

java版

/*
思路:

*/

public class Solution {
    public String reverseStr(String s, int k) {
        char[] arr = s.toCharArray();
        int n = arr.length;
        int i = 0;
        while(i < n) {
            int j = Math.min(i + k - 1, n - 1);
            swap(arr, i, j);
            i += 2 * k;
        }
        return String.valueOf(arr);
    }
    private void swap(char[] arr, int l, int r) {
        while (l < r) {
            char temp = arr[l];
            arr[l++] = arr[r];
            arr[r--] = temp;
        }
    }
}


/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

  1. 翻转字符串里的单词

国内站

java版

/*
思路:

*/




/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

国外站

java版

/*
思路:

*/


public class Solution {
  
  public String reverseWords(String s) {
    if (s == null) return null;
    
    char[] a = s.toCharArray();
    int n = a.length;
    
    // step 1. reverse the whole string
    reverse(a, 0, n - 1);
    // step 2. reverse each word
    reverseWords(a, n);
    // step 3. clean up spaces
    return cleanSpaces(a, n);
  }
  
  void reverseWords(char[] a, int n) {
    int i = 0, j = 0;
      
    while (i < n) {
      while (i < j || i < n && a[i] == ' ') i++; // skip spaces
      while (j < i || j < n && a[j] != ' ') j++; // skip non spaces
      reverse(a, i, j - 1);                      // reverse the word
    }
  }
  
  // trim leading, trailing and multiple spaces
  String cleanSpaces(char[] a, int n) {
    int i = 0, j = 0;
      
    while (j < n) {
      while (j < n && a[j] == ' ') j++;             // skip spaces
      while (j < n && a[j] != ' ') a[i++] = a[j++]; // keep non spaces
      while (j < n && a[j] == ' ') j++;             // skip spaces
      if (j < n) a[i++] = ' ';                      // keep only one space
    }
  
    return new String(a).substring(0, i);
  }
  
  // reverse a[] from a[i] to a[j]
  private void reverse(char[] a, int i, int j) {
    while (i < j) {
      char t = a[i];
      a[i++] = a[j];
      a[j--] = t;
    }
  }
  
}

/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

  1. 反转字符串中的单词 III

国内站

java版

/*
思路:

*/




/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

国外站

java版

/*
思路:

*/

class Solution {
    public String reverseWords(String s) {
        String[] str = s.split(" ");
        for (int i = 0; i < str.length; i++) str[i] = new StringBuilder(str[i]).reverse().toString();
        StringBuilder result = new StringBuilder();
        for (String st : str) result.append(st + " ");
        return result.toString().trim();
    } 
}


/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

  1. 仅仅反转字母

国内站

java版

/*
思路:

*/




/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

国外站

java版

/*
思路:

*/
class Solution {
    public String reverseOnlyLetters(String S) {
        StringBuilder sb = new StringBuilder(S);
        for (int i = 0, j = S.length() - 1; i < j;) {
            if (!Character.isLetter(sb.charAt(i))) {
                ++i;
            } else if (!Character.isLetter(sb.charAt(j))) {
                --j;
            } else {
                sb.setCharAt(i, S.charAt(j));
                sb.setCharAt(j--, S.charAt(i++));
            }
        }
        return sb.toString();
    }
}



/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

  1. 找到字符串中所有字母异位词

国内站

java版

/*
思路:

*/




/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

国外站

java版

/*
思路:

*/

class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> list = new ArrayList<>();
        if (s == null || s.length() == 0 || p == null || p.length() == 0) return list;
        int[] hash = new int[256]; //character hash
        //record each character in p to hash
        for (char c : p.toCharArray()) {
            hash[c]++;
        }
        //two points, initialize count to p's length
        int left = 0, right = 0, count = p.length();
        while (right < s.length()) {
            //move right everytime, if the character exists in p's hash, decrease the count
            //current hash value >= 1 means the character is existing in p
            if (hash[s.charAt(right++)]-- >= 1) count--; 
            
            //when the count is down to 0, means we found the right anagram
            //then add window's left to result list
            if (count == 0) list.add(left);
        
            //if we find the window's size equals to p, then we have to move left (narrow the window) to find the new match window
            //++ to reset the hash because we kicked out the left
            //only increase the count if the character is in p
            //the count >= 0 indicate it was original in the hash, cuz it won't go below 0
            if (right - left == p.length() && hash[s.charAt(left++)]++ >= 0) count++;
        }
        return list;
    }
}


/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

  1. 最长回文子串

国内站

java版

/*
思路:

*/




/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

国外站

java版

/*
思路:

*/

class Solution {
public String longestPalindrome(String s) {
    char[] ca = s.toCharArray();
    int rs = 0, re = 0;
    int max = 0;
    for(int i = 0; i < ca.length; i++) {
        if(isPalindrome(ca, i - max - 1, i)) {
            rs = i - max - 1; re = i;
            max += 2;
        } else if(isPalindrome(ca, i - max, i)) {
            rs = i - max; re = i;
            max += 1;
        }
    }
    return s.substring(rs, re + 1);
}

private boolean isPalindrome(char[] ca, int s, int e) {
    if(s < 0) return false;
    
    while(s < e) {
        if(ca[s++] != ca[e--]) return false;
    }
    return true;
}
}


/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

  1. 同构字符串

国内站

java版

/*
思路:

*/




/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

国外站

java版

/*
思路:

*/

public class Solution {
    public boolean isIsomorphic(String s1, String s2) {
        int[] m = new int[512];
        for (int i = 0; i < s1.length(); i++) {
            if (m[s1.charAt(i)] != m[s2.charAt(i)+256]) return false;
            m[s1.charAt(i)] = m[s2.charAt(i)+256] = i+1;
        }
        return true;
    }
}


/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

  1. 验证回文字符串 Ⅱ

国内站

java版

/*
思路:

*/




/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

国外站

java版

/*
思路:

*/

class Solution {
    public boolean validPalindrome(String s) {
        int l = -1, r = s.length();
        while (++l < --r) 
            if (s.charAt(l) != s.charAt(r)) return isPalindromic(s, l, r+1) || isPalindromic(s, l-1, r);
        return true;
    }

    public boolean isPalindromic(String s, int l, int r) {
        while (++l < --r) 
            if (s.charAt(l) != s.charAt(r)) return false;
        return true;
    }
}


/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

  1. 通配符匹配

国内站

java版

/*
思路:

*/




/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

国外站

java版

/*
思路:

*/

public class Solution {
    public boolean isMatch(String s, String p) {
        boolean[][] match=new boolean[s.length()+1][p.length()+1];
        match[s.length()][p.length()]=true;
        for(int i=p.length()-1;i>=0;i--){
            if(p.charAt(i)!='*')
                break;
            else
                match[s.length()][i]=true;
        }
        for(int i=s.length()-1;i>=0;i--){
            for(int j=p.length()-1;j>=0;j--){
                if(s.charAt(i)==p.charAt(j)||p.charAt(j)=='?')
                        match[i][j]=match[i+1][j+1];
                else if(p.charAt(j)=='*')
                        match[i][j]=match[i+1][j]||match[i][j+1];
                else
                    match[i][j]=false;
            }
        }
        return match[0][0];
    }
}


/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

  1. 最长有效括号

国内站

java版

/*
思路:

*/




/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

国外站

java版

/*
思路:

*/

public class Solution {
    public int longestValidParentheses(String s) {
        Stack<Integer> stack = new Stack<Integer>();
        int max=0;
        int left = -1;
        for(int j=0;j<s.length();j++){
            if(s.charAt(j)=='(') stack.push(j);            
            else {
                if (stack.isEmpty()) left=j;
                else{
                    stack.pop();
                    if(stack.isEmpty()) max=Math.max(max,j-left);
                    else max=Math.max(max,j-stack.peek());
                }
            }
        }
        return max;
    }
}


/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

  1. 不同的子序列

国内站

java版

/*
思路:

*/




/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

国外站

java版

/*
思路:

*/

class Solution {
public int numDistinct(String S, String T) {
    // array creation
    int[][] mem = new int[T.length()+1][S.length()+1];

    // filling the first row: with 1s
    for(int j=0; j<=S.length(); j++) {
        mem[0][j] = 1;
    }
    
    // the first column is 0 by default in every other rows but the first, which we need.
    
    for(int i=0; i<T.length(); i++) {
        for(int j=0; j<S.length(); j++) {
            if(T.charAt(i) == S.charAt(j)) {
                mem[i+1][j+1] = mem[i][j] + mem[i+1][j];
            } else {
                mem[i+1][j+1] = mem[i+1][j];
            }
        }
    }
    
    return mem[T.length()][S.length()];
}
}


/*
评价:
优点:
缺点:
*/

python版:



'''
思路:

'''

'''
评价:
  优点:
  缺点:
'''

c++版:

/*
思路:

*/


/*
评价:
优点:
缺点:
*/

一 字符串基础知识和引申题目

字符串算法_c++

字符串算法_i++_02

字符串算法_i++_03

字符串算法_java_04

字符串算法_字符串_05

字符串算法_字符串_06

字符串算法_i++_07

字符串算法_c++_08

字符串算法_python_09

字符串算法_i++_10

字符串算法_字符串_11

二 高级字符串算法

字符串算法_i++_12

字符串算法_i++_13

字符串算法_i++_14

字符串算法_python_15

字符串算法_java_16

三 字符串匹配算法

字符串算法_python_17

字符串算法_i++_18

字符串算法_字符串_19

字符串算法_python_20

字符串算法_i++_21

字符串算法_字符串_22

字符串算法_c++_23