1

I am trying to delete character at a time from a string in Java this is what I wrote

String word = "evelina";
char[] wordCharArr = word.toCharArray();

//Deleting one

for(int i = 0; i< wordCharArr.length; i++)
{
    String answer = word.subString(i);
    if(list.lookup(answer))
        perm.add(answer);
}

What this does is this:

evelina
velina
elina
lina
ina
na
a

But I need it to do this

evelina
velina
eelina
evlina
eveina
evelna
evelia
evelin

1 Answer 1

3

At each iteration, you need to skip the i-th character:

for (int i = 0; i < wordCharArr.length; i++) {
  String answer = word.substring(0, i) + word.substring(i + 1);
  if (list.lookup(answer)) {
    perm.add(answer);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.