# 算法面试刷题之字符串

``````String str="hello";
str.toUpperCase();
``````

### 字符串常见面试题

##### 最大相邻的非重复的字符串长度

``````   public int lengthOfLongestSubstring(String s) {
int leftBound=0;
int max=0;
HashMap<Character,Integer> map=new HashMap<Character,Integer>();
for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
leftBound=Math.max(leftBound, map.containsKey(ch)?map.get(ch)+1:0);
max=Math.max(max, i-leftBound+1);
map.put(ch, i);
}
return max;
}``````
###### 乱序字符串

`````` public List<String> anagrams(String[] strs) {
List<String> re=new ArrayList<>();
List<String> result=new ArrayList<>();
if(strs.length==0){
return re;
}
for(int i=0;i<strs.length;i++){
}
for(int i=0;i<strs.length;i++){
String index=strs[i];
if(re.indexOf(fromArrayToString(index))!=re.lastIndexOf(fromArrayToString(index))){
}
}
return result;
}
public static String fromArrayToString(String str){
char[] arrays=str.toCharArray();
Arrays.sort(arrays);
String re=String.copyValueOf(arrays);//注意如果想要把char数组转化为String需要使用String.copyValueof函数，而不是arrays.toString（）
return re;
}``````
##### 翻转字符串

For example,
Given s = “the sky is blue”,
return “blue is sky the”.

`````` public String reverseWords(String s) {
String re="";

String[] words=s.split(" ");

for(int i=words.length-1;i>=0;i--){
re=re+words[i]+" ";
}
if(re==""){
return re;
}else{
return re.substring(0, re.lastIndexOf(" "));
}
}``````
##### 判断一个字符串是否为回文串

``````    if(s==""){
return true;
}
String b=s.replaceAll("[^0-9A-Za-z]", "");
String a=b.toLowerCase();
for(int i=0;i<a.length()/2;i++){
if(a.charAt(i)!=a.charAt(a.length()-1-i)){
return false;
}
}
return true;
}``````