English 中文(简体)
从字符串中删除重复字符, 并使用递归只保留单个事件
原标题:Remove duplicate character from a string and keep only single occurrences using recursion

我尝试从字符串中删除多处字符, 并仅保留一次( 第一次), 使用循环 。 重复可以是连续的, 也可以是任意的 。

注:我没有使用任何类型的收藏。

e.g. Input:- RAMA, Output:- RAM i.e It removed only second occurrence of char A and printed RAM . I am getting stuck at calling recursive function remove with stack values as it is always being called with same value A . Please review once.

这是我试过的密码

class RecursionRemoveDuplicatesFromString {
    String newStr="";
    String subs = "";
    String remvDupl = "";
    int i = 0;
    int j = 0;
    int count = 0;
    String removeDuplicates(String toSubs){
        //String toSubs = s;
        if(toSubs.length()==1){
            return toSubs;
        }
        
        if(i <= toSubs.length()-2){
            subs = toSubs.substring(i,i+1);
        }else{
            subs = toSubs.substring(i);
            return subs;
        }
        //String subs = i <= toSubs.length()-2 ? toSubs.substring(i,i+1) : return toSubs.substring(i,i+1);
        i++;
        System.out.println(subs);
        removeDuplicates(toSubs);
        //System.out.println(subs);
        remove(toSubs,subs);
        return newStr;
    }
    void remove(String s, String subs){
        if(s.length()==1){
            return;
        }
        while(j<s.length()){

            if(j <= s.length()-2){
                remvDupl = s.substring(j,j+1);
            }else{
                remvDupl = s.substring(j);
                //return remvDupl;
            }
            //String remvDupl = j < s.length()-2 ? s.substring(j,j+1) : "";
            if(remvDupl.equalsIgnoreCase(subs)){
                count += 1;
                //newStr += subs;
                if(count==1){
                    newStr += subs;
                }
            }
            j++;
    }
        //remove(s, remvDupl);
        count = 0;
    }
    
    public static void main(String[] args) {
        String s = "RAMA";
        
        RecursionRemoveDuplicatesFromString remvDupli = new RecursionRemoveDuplicatesFromString();
        System.out.println(remvDupli.removeDuplicates(s));
        
    }
}
问题回答

链接的HashSet确保插入顺序。

import java.util.LinkedHashSet;
public class Main {
    public static void main(String[] args) {
        String s = "RAMA";
        LinkedHashSet<String> set = new LinkedHashSet<>();
        char[] c1=s.toCharArray();
        for(int i=0;i< c1.length;i++){
            set.add(String.valueOf(c1[i]));
        }
        System.out.println(set); //[R, A, M]
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签