How to find possible permutations of a given Word in java

How to find possible permutations of a given Word in java
SOURCE : PlaindromePemutations.java

import java.util.HashSet;

public class PlaindromePemutations {

public static void main(String[] args) {
String str = "malayalam";

HashSet hs = new HashSet<>();
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j <= str.length(); j++) {

if (isplaind(str.substring(i, j)))
hs.add(str.substring(i, j));

}
}
System.out.println(hs);

}

private static boolean isplaind(String str) {

if (str.length() > 0) {

for (int i = 0, j = str.length() - 1; i <= j; i++, j--) {

if (str.charAt(i) == str.charAt(j)) {
continue;

} else
return false;
}
return true;

}

return false;
}
}


INPUT

String str = "malayalam";



OUTPUT

[layal, a, malayalam, alayala, ala, aya, y, l, m]






Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+




SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment