劍指Offer28:常見的三個字符串問題

常見的三個字符串問題

問題1、從第一個字符串中刪除在第二個字符串中出現的所有字符。

問題描述

定義一個函數,輸入兩個字符串,從第一個字符串中刪除在第二個字符串中出現的所有字符。

public class Code028_05 {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String str1=sc.nextLine();
        String str2=sc.nextLine();
        removeSubString(str1,str2);
    }
    private static void removeSubString(String str1,String str2){
        if(str1.length()==0 || str2.length()==0){
            return;
        }
        ArrayList<Character> list=new ArrayList<>();
        for(int i=0;i<str1.length();i++){
            if(!(str2.contains(str1.charAt(i)+""))){
                list.add(str1.charAt(i));
            }
        }
        for(int k=0;k<list.size();k++){
            System.out.print(list.get(k)+"");
        }
    }
}

問題2、刪除字符串中所有重複出現的字符

public class Code028_05 {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String str1=sc.nextLine();
        String str2=sc.nextLine();
        removeRepeat(str1);
    }
    private static void removeRepeat(String string){
        if(string.length()==0){
            return;
        }
//        Vector<Character> vector=new Vector<>();
//        for(int i=0;i<string.length();i++){
//            if(!vector.contains(string.charAt(i))){
//                vector.add(string.charAt(i));
//            }
//        }
//        for(int i=0;i<vector.size();i++){
//            System.out.print(vector.get(i)+"");
//        }
        HashMap<Integer,Character> map=new HashMap<>();
        for(int i=0;i<string.length();i++){
            if(!map.containsValue(string.charAt(i))){
                map.put(i,string.charAt(i));
            }
        }
//        for(char ch:map.values()){
//            System.out.print(ch+"");
//        }
        for(int key:map.keySet()){
            System.out.print(map.get(key)+"");
        }
    }
}

問題3、變位詞問題

問題描述

在英語中,如果兩個單詞出現的字母相同,並且每個字母出現的次數也相同,那麼兩個單詞護衛變位詞。定義函數,是否互爲變位詞。輸入兩個字符串,判斷是否互爲變位詞

public class Code028_05 {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String str1=sc.nextLine();
        String str2=sc.nextLine();
        boolean result=isVariant(str1,str2);
        System.out.print(result);
    }
    private static boolean isVariant(String str1,String str2){
        if(str1.length()==0 || str2.length()==0 ||str1.length()!=str2.length()){
            return false;
        }
        HashMap<Character,Integer> map=new HashMap<>();
        for(int i=0;i<str1.length();i++){
            if(map.containsKey(str1.charAt(i))){
                int value=map.get(str1.charAt(i));
                value+=1;
                map.put(str1.charAt(i),value);
            }else {
                map.put(str1.charAt(i),1);
            }
        }
        for(int i=0;i<str2.length();i++){
            if(map.containsKey(str2.charAt(i))){
                int value2=map.get(str2.charAt(i));
                value2-=1;
                map.put(str2.charAt(i),value2);
            }else {
                return false;
            }
        }
        for(int v:map.values()){
            if(v>0){
                return false;
            }
        }
        return true;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章