# Common algorithm topics

### 1. Simulate one trim Method , Remove the spaces at both ends of the string .

``````//1. Simulate one trim Method , Remove the spaces at both ends of the string .
public class Test1 {
public static void main(String[] args) {
String str1="   has a dog   ";
String str2=mytrim(str1);
System.out.println("*********"+str2+"*******");
}

public static String mytrim(String str){
if(str==null||str.equals("")){
return "";
}

int startIndex=0;
int endIndex=str.length();

char[] strCharArray=str.toCharArray();

for(int i=0;i<strCharArray.length;i++){
if(!Character.isSpaceChar(strCharArray[i])){
startIndex=i;
break;
}

if(i==strCharArray.length-1){
return "";
}
}

for(int j=strCharArray.length-1;j>=0;j--){
if(!Character.isSpaceChar(strCharArray[j])){
endIndex=j+1;
break;
}
}

return str.substring(startIndex,endIndex);
}
}
``````

Output results ：

``````*********has a dog*******
``````

### 2. Invert a string . Inverts the specified part of a string .

such as “abcdefg” Reversal is “abfedcg”.

``````/*
2. Invert a string . Inverts the specified part of a string .
such as “abcdefg” Reversal is “abfedcg”.
*/
public class Test2 {
public static void main(String[] args) {
String str1 = new String("abcdefg");
String str2=reverse(str1,2,5);
System.out.println(str2);
}

public static String reverse(String str, int startIndex, int endIndex) {
if (str == null || str.equals("")) {
return "";
}

char[] strArr = str.toCharArray();

if (startIndex > str.length() || startIndex < 0 || endIndex < startIndex || endIndex > str.length()) {
return " Wrong index ";
}

for (int i = startIndex, j = endIndex-1; i < j; i++, j--) {
char tmp = strArr[i];
strArr[i] = strArr[j];
strArr[j] = tmp;
}

return new String(strArr);
}
}

``````

Output results ：

``````abedcfg
``````

### 3. Gets the number of times a string appears in another string .

such as ： obtain “ab” stay abkkcadkabkebkabkskab“ Is the number of times .

``````/*
3. Gets the number of times a string appears in another string .

such as ： obtain “ab” stay abkkcadkabkebkabkskab“ Is the number of times .
*/
public class Test3 {
public static void main(String[] args) {
int count=getCount(str1,"ab");
System.out.println(count);

}

public static int getCount(String str1,String str2){
int count=0;

if(str2.length()>str1.length()){
return -1;
}

for (int i = 0; i <=(str1.length()-str2.length()) ; i++) {
if(str1.startsWith(str2,i)){
count++;
}
}

return count;
}
}

``````

Output results

``````4
``````

### 4. Gets the largest identical substring of two strings .

such as ：str1=“abcwertthelloyuiodef”;str2=“cvhellobnm”
Tips ： Compare the substring of the shorter string with the substring of the longer string .

``````/*
4. Gets the largest identical substring of two strings .

such as ：str1=“abcwertthelloyuiodefabcde”;str2=“cvhellobnmabcde”
Tips ： Compare the substring of the shorter string with the substring of the longer string .
*/
public class Test4 {
public static void main(String[] args) {
String str1="abcwertthelloyuiodefabcde";
String str2="cvhellobnmabcde";
String[] sameArr=findSameStr(str1,str2);
System.out.println(Arrays.toString(sameArr));
}

public static String[] findSameStr(String maxStr,String minStr){

if(maxStr.length()<minStr.length()){
String temp=minStr;
minStr=maxStr;
maxStr=temp;
}

StringBuilder sameStr=new StringBuilder();

for (int i=minStr.length();i>0;i--){
for(int j=0;j+i<=minStr.length();j++){
String sub=minStr.substring(j,j+i);
if(maxStr.contains(sub)){
sameStr.append(sub+",");
}
}
if(sameStr.length()!=0){
break;
}
}

return sameStr.toString().split(",");
}
}

``````

Output results

``````[hello, abcde]
``````

### 5. Sort the characters in a string in natural order .

Tips ：

1. A string becomes an array of characters .

2. Sort the array , choice , Bubbling ,Arrays.sort();

3. Turn the sorted array into a string .

``````public class Test5 {
public static void main(String[] args) {
String str2=sortString(str1);
System.out.println(str2);
}

public static String sortString(String str){
char[] strArr=str.toCharArray();

Arrays.sort(strArr);

return new String(strArr);
}
}

``````

Output results

``````1234aaaaaddffkkks
``````

https://cdmana.com/2021/05/20210524224154505u.html

Scroll to Top