[Interview algorithm questions] Java-String algorithm

Interview questions:
1. Simulate a trim method to remove the spaces at both ends of the string.
2. Reverse a character string. Reverse the specified part of the string. For example, "abcdefg" is reversed to "abfedcg".

//将一个字符串进行反转。将字符串中指定部分进行反转。比如“abcdefg”反转为“abfedcg”。
public class ReverseTest {
    
    
    @Test
    public void test() {
    
    
        String str = "abcdefg";
        String r = reverse(str, 2, 5);
        System.out.println(r);
    }

    //方式一:转换为char[]
    public String reverse(String str, int startIndex, int endIndex) {
    
    
        if (str != null ) {
    
    
            char[] c = str.toCharArray();
            for (int x = startIndex, y = endIndex; x < y; x++, y--) {
    
    
                char temp = c[x];
                c[x] = c[y];
                c[y] = temp;
            }
            return new String(c);
        }
        return null;
    }
    //方式二:使用String拼接
    public String reverse1(String str, int startIndex, int endIndex) {
    
    
        if (str != null) {
    
    
            //第一部分
            String reverseStr = str.substring(0, startIndex);
            //第二部分
            for (int i = startIndex; i >= startIndex; i--) {
    
    
                reverseStr += str.charAt(i);
            }
            //第三部分
            reverseStr += str.substring(endIndex + 1);
            return reverseStr;
        }
        return null;
    }
}

3. Get the number of occurrences of a string in another character. (For example: get the number of occurrences of "ab" in "abkkcadkabkekafkabkskab").

/*
 *  获取一个字符串在另一个字符串中出现的次数。
 *  比如:获取“ab”在“abkkcadkabkebfkaabkskab”中出现的次数
 */
public class FindTest {
    
    
    public int getCount(String mainStr,String subStr){
    
    
        int mainLength = mainStr.length();
        int subLength = subStr.length();
        int count = 0;
        int index = 0;
        if (mainLength >= subLength){
    
    
            while ((index = mainStr.indexOf(subStr,index)) != -1){
    
    
                count++;
                index += subLength;
            }
            return count;
        }else {
    
    
            return 0;
        }
    }

    @Test
    public void test(){
    
    
        String mainStr = "abkkcadkabkebfkaabkskab";
        String subStr = "ab";
        int count = getCount(mainStr,subStr);
        System.out.println(count);
    }
}

4. Get the largest identical substring in the two strings. For example: str1 = "abcwerthellotuiodef"; str2 = "cvhellobnm". Hint: Compare the shorter string with the longer string with decreasing length.

/*
 *  获取两个字符串中最大相同子串。比如:
 *  str1="abcwerthelloyuiodef";str2="cvhellobnm"
 *  提示:将短的那个串进行长度依次递减的子串与较长的串比较。
 */
public class MaxSameTest {
    
    
    /*//前提:字符串中只有一个最大相同值
    public String getMaxSameSubString(String str1, String str2) {
        if (str1 != null && str2 != null){
            String maxStr = str1.length() >= str2.length() ? str1 : str2;
            String minStr = str1.length() < str2.length() ? str1 : str2;
            int length = minStr.length();

            for (int i = 0; i < length; i++) {
                for (int x = 0, y = length - i; y < length; x++, y++) {
                    String subStr = minStr.substring(x, y);
                    if (maxStr.contains(subStr)) {
                        return subStr;
                    }
                }
            }
        }
        return null;
    }*/

    //情况:字符串中有多个相同串
    public String[] getMaxSameSubString1(String str1, String str2) {
    
    
        if (str1 != null && str2 != null) {
    
    
            StringBuffer sBuffer = new StringBuffer();
            String maxString = (str1.length() >= str2.length()) ? str1 : str2;
            String minString = (str1.length() < str2.length()) ? str1 : str2;
            int len = minString.length();

            for (int i = 0; i < len; i++) {
    
    
                for (int x = 0, y = len - i; y <= len; x++, y++) {
    
    
                    String subString = minString.substring(x, y);
                    if (maxString.contains(subString)) {
    
    
                        sBuffer.append(subString + ",");
                    }
                }
//                System.out.println(sBuffer);
                if (sBuffer.length() != 0) {
    
    
                    break;
                }
            }
            String[] split = sBuffer.toString().replaceAll(",$", "").split("\\,");
            return split;
        }
        return null;
    }

    @Test
    public void test() {
    
    
        String str1 = "abcwerthello1yuiodefabcdef";
        String str2 = "cvhello1bnmabcdef";
        String[] maxSameSubStrings = getMaxSameSubString1(str1, str2);
        System.out.println(Arrays.toString(maxSameSubStrings));
    }
}

5. Sort the characters in the string in natural order. Tips: ①The string becomes an array of characters. ② Sort the array, select, bubble, Arrays.sort(); ③ Turn the sorted array into a string.

Guess you like

Origin blog.csdn.net/qq_30068165/article/details/115179515