package com.lovecoding.day17.ex01;

import org.junit.Test;

import java.util.Arrays;

public class Test02 {

    //1个字符串中可能包含a-z中的多个字符，字符也可能重复，例如：String data = “aabcexmkduyruieiopxzkkkkasdfjxjdsds”;
    //写一个程序，对于给定一个这样的字符串求出字符串出现次数最多的那个字母以及出现的次数（若次数最多的字母有多个，则全部求出）
    //提示：
    //- 数组 长度 26  0索引 a 1代表b   int
    //- 字符-97 == 0   该位置++
    @Test
    public void test04(){
        //26数组
        int[] arr = new int[26];

        //字符串
        String str = "aaaabcexmkduyruieiopxzkkkkasdfjxjdsds";

        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            arr[chars[i]-97]++;
        }
        //arr
        //[3, 1, 1, 4, 2, 1, 0, 0, 2, 2, 5, 0, 1, 0, 1, 1, 0, 1, 3, 0, 2, 0, 0, 3, 1, 1]
        // a  b  c  d  e  f  g  h  i  j  k
        System.out.println(Arrays.toString(arr));

        //找最值
        int max = arr[0];
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]>max){
                max = arr[i];
                index = i;
            }
        }
//        System.out.println("max："+max);
//
//        System.out.println((char) (index+97));

        //
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]==max){
                System.out.println("max："+arr[i]);
                System.out.println((char) (i+97));
            }
        }
    }


    //获取一个字符串在另一个字符串中出现的次数。比如：获取"ab"在 “abababkkcadkabkebfkabkskab”中出现的次数
    @Test
    public void test03(){
        int count = count1("ab", "abababkkcadkabkebfkabkskab");
        System.out.println(count);
    }
    private int count1(String mstr, String maxstr ) {
        int length = maxstr.replaceAll(mstr, "").length();
        return (maxstr.length() - length) / mstr.length();
    }

    private int count(String mstr, String maxstr ) {

        int count = 0;

        while (maxstr.contains(mstr)){
            maxstr = maxstr.replaceFirst(mstr, "");
            count++;
        }
        return count;
    }

    //将字符串中指定部分进行反转。比如将“abcdefgho”实现部分反转，结果为”abfedcgho”
    @Test
    public void test02(){
        String s1 = "helloworld";
        String resver = resver2(s1, 1, 5);
        System.out.println(resver);
    }
    public String resver2(String str,int s , int e){
        String left = str.substring(0, s);
        String m = str.substring(s, e);
        String right = str.substring(e, str.length());

        //StringBuffer
        StringBuffer sb = new StringBuffer(m);
        StringBuffer reverse = sb.reverse();
        return left + reverse.toString() + right;
    }

    public String resver1(String str,int s , int e){

        char[] chars = str.toCharArray();

        for (int i = s , j = e; i <e; i++,j--) {
            char temp = chars[i];
            chars[i] = chars[j];
            chars[j] = temp;
        }

        String s1 = new String(chars);
        return s1;
    }

    //不能使用trim()，实现去除字符串两端的空格。
    @Test
    public void test01(){
        //比较空串和字符' '
        // str.startsWith(" ")
        String s1 = "   hell   oworld    ";
        String s = trim03(s1);
        System.out.println(s);
    }

    public String trim03(String str){
        String s = str.replaceAll("(^\\s+)|(\\s+$)", "");
        return s;
    }

    public String trim02(String str){

        while ( str.startsWith(" ") ){
            str = str.replaceFirst(" ", "");
        }
        while ( str.endsWith(" ") ){
            str = str.substring(0,str.length()-1);
        }
        return str;
    }

    public String trim01(String str){
        char[] chars = str.toCharArray();
        int start = 0;
        for (int i = 0; i < chars.length; i++) {
            //字符串和cahr比较
            if (String.valueOf(chars[i]).equals(" ")){
                start++;
            }else {
                break;
            }
        }
        int end = chars.length;

        for (int i = chars.length-1; 0 < chars.length; i--) {
            if (" ".equals(String.valueOf(chars[i]))){
                end--;
            }else {
                break;
            }
        }

        String substring = str.substring(start, end);
        return substring;
    }

}
