I came across a post showing how to arrange char array by alphabet order.
seeing this can be done, I want to output the alphabetical order of each character of the input string, in order of the characters of the input string.
I'm a bit stuck. I can get the string reordered alphabetically, but I don't know what to do next.
example is 'monkey' to '354216'
because 'ekmnoy' e is alphabetically first from the set of given characters so e = 1 , k is the second alpha char when sorted so k = 2, and so on.
if you cannot understand I can provide more example to make things clear out.
Code
String str = "airport";
Character[] chars = new Character[str.length()];
for (int z = 0; z < chars.length; z++) {
chars[z] = str.charAt(z);
}
Arrays.sort(chars, new Comparator<Character>() {
public int compare(Character c1, Character c2) {
int cmp = Character.compare(
Character.toLowerCase(c1.charValue()),
Character.toLowerCase(c2.charValue()));
if (cmp != 0) {
return cmp;
}
return Character.compare(c1.charValue(), c2.charValue());
}
});
StringBuilder sb = new StringBuilder(chars.length);
for (char c : chars) {
sb.append(c);
}
str = sb.toString();
System.out.println(sb);
Output
aioprrt
expected output
Orange -> aegnOr
561432 - 123456
Monkey -> ekMnoy
354216 -> 123456
I dont know what you want to do with double characters, but if you add this few lines to your code at the end you are getting the right result. Iterate over the sorted String and replace the charakters in the original String with their indices in the sorted String.
String originalStr = "airport";
for(int i = 0; i<str.length(); i++) {
originalStr = originalStr.replace(str.charAt(i), String.valueOf(i+1).charAt(0));
}
System.out.println(originalStr);
Output: 1254357
If you want to get the output: 1254367 use replaceFirst:
originalStr = originalStr.replaceFirst(String.valueOf(str.charAt(i)), String.valueOf(i+1));
Input:Orange
Output:561432
Input:Monkey
Output:354216
The whole code:
String str = "airport";
String originalStr = str; //creat a backup of str because you change it in your code
Character[] chars = str.toCharArray();
Arrays.sort(chars, new Comparator<Character>() {
public int compare(Character c1, Character c2) {
int cmp = Character.compare(
Character.toLowerCase(c1.charValue()),
Character.toLowerCase(c2.charValue()));
if (cmp != 0) {
return cmp;
}
return Character.compare(c1.charValue(), c2.charValue());
}
});
str = String.valueOf(chars);
System.out.println(str);
//Iterate over the sorted String and replace the charakters in the original String with their indices in the sorted String
for(int i = 0; i<str.length(); i++) {
originalStr = originalStr.replaceFirst(String.valueOf(str.charAt(i)), String.valueOf(i+1));
}
System.out.println(originalStr);
Once you have arranged the characters in order (in a different array from the original) then create a third array by walking the original string and choosing the index of each character from te sorted string.
input: edcba
sorted: abcde
index: 01234
Pseudocode...
for( int i = 0; i < input.length(); i++ ) {
index[i] = sorted.indexOf(input[i]);
}
Result should be 43210 with the given input.
Note that strings with more than 10 characters will result in ambiguous output, which can be handled by inserting spaces in the output. Example:
abcdefghijk ->
012345678910
You can use this below code:
package Test;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
public class Arrange {
public static void main(String[] args) {
String str = "money";
List<Test> strs=new LinkedList<Test>();
List<Test> final_result=new LinkedList<Test>();
for(int i=0;i<str.length();i++)
{
Test t=new Test(i, ""+str.charAt(i), 0);
strs.add(t);
}
Collections.sort(strs,new Comparator<Test>() {
#Override
public int compare(Test o1, Test o2) {
return (o1.getS().compareToIgnoreCase(o2.getS()));
}
});
Integer i=1;
for (Test st : strs) {
st.setJ(i);
final_result.add(st);
i++;
}
Collections.sort(final_result,new Comparator<Test>() {
#Override
public int compare(Test o1, Test o2) {
return (o1.getI().compareTo(o2.getI()));
}
});
for (Test test : final_result) {
System.out.println(test.getJ());
}
}
}
class Test{
private Integer i;
private String s;
private Integer j;
public Test() {
// TODO Auto-generated constructor stub
}
public Test(Integer i, String s, Integer j) {
super();
this.i = i;
this.s = s;
this.j = j;
}
public Integer getI() {
return i;
}
public void setI(Integer i) {
this.i = i;
}
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
public Integer getJ() {
return j;
}
public void setJ(Integer j) {
this.j = j;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((i == null) ? 0 : i.hashCode());
result = prime * result + ((j == null) ? 0 : j.hashCode());
result = prime * result + ((s == null) ? 0 : s.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Test other = (Test) obj;
if (i == null) {
if (other.i != null)
return false;
} else if (!i.equals(other.i))
return false;
if (j == null) {
if (other.j != null)
return false;
} else if (!j.equals(other.j))
return false;
if (s == null) {
if (other.s != null)
return false;
} else if (!s.equals(other.s))
return false;
return true;
}
}
Related
I have a task to find four unique elements , sum of which is defined. So I have as input data : data array of n elemeents, elements can be duplicated, and 's' is sum.
I have two cycles , first i in values [0, n-1], second j in [i+1, n]. All unique pairs of elements I save in Map , where key is sum and value is Collections of possible elements whats consists that sum. Result is collection of four unique elements of input data array. All actions I do in second cycle :
I check if I already have a differents beetween 's' and data[i]+data[j] ,
2)if I have I check , if data[i] and data[j] doesn't coinside with elements from saved pairs and add to reslut.
Add this pair data[i] + data [ j] to Map with history
I have a Memory Limit in this task and I get over it. Time limit is O(n^2). As I undestand I do some extra actions and save some unnecessary data.I created two object Fourths and Pairs , but they has only primitive fields inside so I thinK what deal is not in that
Here is my code in java:
public class SunForthFAIL {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
int s = Integer.parseInt(reader.readLine());
int[] data = new int[n];
Set<Forths> result = new HashSet<>();
Map<Integer, Set<Pair>> history = new HashMap<>();
StringTokenizer stringTokenizer = new StringTokenizer(reader.readLine());
for (int i = 0; i < n; i++) {
data[i] = Integer.parseInt(stringTokenizer.nextToken());
}
Arrays.sort(data);
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int sum = data[i] + data[j];
int target = s - sum;
if (history.containsKey(target)) {
for (Pair historyPair : history.get(target)) {
if (historyPair.isDiff(i, j)) {
result.add(new Forths(historyPair.getiValue(), historyPair.getjValue(), data[i], data[j]));
}
}
}
if (history.containsKey(sum)) {
history.get(sum).add(new Pair(i, j, data[i], data[j]));
} else {
Set<Pair> set = new HashSet<>();
set.add(new Pair(i, j, data[i], data[j]));
history.put(data[i] + data[j], set);
}
}
}
System.out.println(result.size());
result.stream().sorted(Comparator.comparingInt(Forths::getFirst).thenComparing(Comparator.comparingInt(Forths::getSecond))).forEach(x -> System.out.println(x));
}
}
class Pair {
private int i;
private int j;
private int iValue;
private int jValue;
public int getiValue() {
return iValue;
}
public int getjValue() {
return jValue;
}
public Pair(int i, int j, int iValue, int jValue) {
this.i = i;
this.j = j;
this.iValue = iValue;
this.jValue = jValue;
;
}
public boolean isEquel(int i, int j) {
if (Math.min(iValue, jValue) == Math.min(i, j) &&
Math.max(iValue, jValue) == Math.max(i, j))
return true;
else
return false;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
if (Math.min(iValue, jValue) == Math.min(pair.iValue, pair.jValue) &&
Math.max(iValue, jValue) == Math.max(pair.iValue, pair.jValue))
return true;
else
return false;
}
#Override
public int hashCode() {
return Objects.hash(Math.min(iValue, jValue) + " " + Math.max(iValue, jValue));
}
public boolean isDiff(int i, int j) {
if (this.i == i || this.i == j || this.j == i || this.j == j)
return false;
else
return true;
}
}
class Forths {
private int first;
private int second;
private int third;
private int forth;
public int getFirst() {
return first;
}
public int getSecond() {
return second;
}
public Forths(int a, int b, int c, int d) {
int[] arr = new int[]{a, b, c, d};
Arrays.sort(arr);
this.first = arr[0];
this.second = arr[1];
this.third = arr[2];
this.forth = arr[3];
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Forths forths = (Forths) o;
return first == forths.first && second == forths.second && third == forths.third && forth == forths.forth;
}
#Override
public int hashCode() {
return Objects.hash(first, second, third, forth);
}
#Override
public String toString() {
return first + " " + second + " " + third + " " + forth;
}
}
In my case, I change the Pair object to int[2], with indexes of pair elements. And memory limit issue is resolved.
I'm trying to learn java using the basic tictactoe example, but just for fun I wanted to design an ai later.
I'm having an issue flattening the char[] to Array. I'm so confused is there a better way to do this?
Do I need to create another method to specific convert this char[] to array?
ERROR:
The method mapToCharater(Charater.class::cast) is undefined for the type Stream<Object>
CONSOLE OUTPUT
EDIT:
Exception in thread "main" java.lang.ClassCastException: Cannot cast [C to java.lang.Character
at java.base/java.lang.Class.cast(Class.java:3610)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.stream.Streams$StreamBuilderImpl.forEachRemaining(Streams.java:411)
at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658)
at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:274)
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:550)
at java.base/java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260)
at java.base/java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:517)
at java.base/java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:523)
at aiPackage.game.Board.<init>(Board.java:17)
at aiPackage.game.Tester.main(Tester.java:15)
package aiPackage.game;
import java.util.*;
import java.util.stream.*;
public class Board {
//declaration of private members
private int score;
private Board previousB = null;
private char[][] thisBoard;
// ------------------------------------------------------;
public Board (char [][] inBoard) {
//what
//char[] flats = flatten(inBoard).map
Object[] flats = flatten(inBoard).map(Character.class::cast).toArray(); //mapToCharater(Charater.class::cast).toArray();
int[] flat = flatten(inBoard).mapToInt(Integer.class::cast).toArray();
int flatSize = flat.length;
// ------------------------------------------------------;
//check if square
if (Math.sqrt(flatSize)==3) {
if(inBoard.length == 3) {
thisBoard = inBoard;
}
else {
System.out.println("The array isnt a square.");
}
}
else {
System.out.println("It doesnt match the dimensions of a tictactoe board.");
}
//we'll assume its not gonna break from the input atm
setThisBoard(inBoard);
}
//https://stackoverflow.com/questions/31851548/flatten-nested-arrays-in-java
private static Stream<Object> flatten(Object[] array) {
return Arrays.stream(array)
.flatMap(o -> o instanceof Object[]? flatten((Object[])o): Stream.of(o));
}
public Board getPreviousB() {
return previousB;
}
public void setPreviousB(Board previousB) {
this.previousB = previousB;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public char[][] getThisBoard() {
return thisBoard;
}
public void setThisBoard(char[][] thisBoard) {
this.thisBoard = thisBoard;
}
//if there are even elements on the board, its x's turn
public ArrayList<Board> getChildren(){
return null;
}
public void checkIfEnded() {
for (int i = 0; i < 3; i++) {
//check row wins
if (thisBoard[i][0] != '-' &&
thisBoard[i][0] == thisBoard[i][1] &&
thisBoard[i][1] == thisBoard[i][2]) {
//updates score based on winner
if (thisBoard[0][2] == 'x') {
updateScore(1);
}
else {
updateScore(-1);
}
return;
}
//check column wins
if (thisBoard[i][0] != '-' &&
thisBoard[0][i] == thisBoard[1][i] &&
thisBoard[1][i] == thisBoard[2][i]) {
//updates score based on winner
if (thisBoard[0][2] == 'x') {
updateScore(1);
}
else {
updateScore(-1);
}
return;
}
}
//check diagnals
if (thisBoard[0][0] != '-' &&
thisBoard[0][0] == thisBoard[1][1] &&
thisBoard[1][1] == thisBoard[2][2]) {
//updates score based on winner
if (thisBoard[0][2] == 'x') {
updateScore(1);
}
else {
updateScore(-1);
}
return;
}
if (thisBoard[0][2] != '-' &&
thisBoard[0][2] == thisBoard[1][1] &&
thisBoard[1][1] == thisBoard[2][0]) {
//updates score based on winner
if (thisBoard[0][2] == 'x') {
updateScore(1);
}
else {
updateScore(-1);
}
return;
}
}
//outputs the board's contents as a string
public String toString() {
String result = "";
//gets the previous board's info to output first
result = "" + previousB;
//gets this boards info to output
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
result += thisBoard[i][j] + " ";
}
//adds newline char at end of row
result += "\n";
}
//adds an extra newline char at end of board
result += "\n";
return result;
}
private void updateScore(int win) {
if (win > 0) {
score = 1;
} else if(win == 0){
score = 0;
}else {
score = -1;
}
}
}
package aiPackage.game;
import java.util.*;
public class Tester {
private static Board start;
private static Board finish;
public static void main(String[] args) {
char temp [][] = {
{'o','-','x'},
{'-','-','-'},
{'-','-','-'}};
start = new Board(temp);
finish = minMax(start.getChildren());
System.out.println();
}
public static Board minMax(ArrayList<Board> resultList) {
return null;
}
}
The reason it fails is because Arrays.stream() does not accept char[] The following should work.
Arrays.stream(inBoard)
.flatMap(x -> (Stream<Character>)new String(x).chars().mapToObj(i->(char)i))
.collect(Collectors.toList())
check out
Want to create a stream of characters from char array in java for
and
Why is String.chars() a stream of ints in Java 8?
for better understanding.
Your problem is here:
private static Stream<Object> flatten(Object[] array) {
return Arrays.stream(array)
.flatMap(o -> o instanceof Object[]? flatten((Object[])o): Stream.of(o));
}
You are using the comparation with Object[] to decompose the array, but in reality is a char[].
That makes that your result instead of being an Stream Object is a Stream char[].
The main point is that you can't use a Stream to work with primitives. Stream only works with Objects and char is a primitive.
you can manually flatten the char[][] using a classic loop.
flattening char[][] or impossible doesn't count
char[] array = Stream.of( inBoard ).flatMap( arr -> Stream.of( String.valueOf( arr ) ) )
.collect( Collectors.joining() ).toCharArray(); // [o, -, x, -, -, -, -, -, -]
''Given two string s and t, write a function to check if s contains all characters of t (in the same order as they are in string t).
Return true or false.
recursion not necessary.
here is the snippet of code that I am writing in java.
problem is for input: string1="st3h5irteuyarh!"
and string2="shrey"
it should return TRUE but it is returning FALSE. Why is that?''
public class Solution {
public static String getString(char x)
{
String s = String.valueOf(x);
return s;
}
public static boolean checkSequence(String s1, String s2)
{
String a = getString(s1.charAt(0));
String b = getString(s2.charAt(0));
for (int i = 1; i < s1.length(); i++)
if (s1.charAt(i) != s1.charAt(i - 1))
{
a += getString(s1.charAt(i));
}
for (int i = 1; i < s2.length(); i++)
if (s2.charAt(i) != s2.charAt(i - 1))
{
b += getString(s2.charAt(i));
}
if (!a.equals(b))
return false;
return true;
}
}
This is a solution:
public class Solution {
public static String getString(char x)
{
String s = String.valueOf(x);
return s;
}
public static boolean checkSequence(String s1, String s2)
{
String a = getString(s1.charAt(0));
String b = getString(s2.charAt(0));
int count = 0;
for (int i = 0; i < s1.length(); i++)
{
if (s1.charAt(i) == s2.charAt(count))
{
count++;
}
if (count == s2.length())
return true;
}
return false;
}
}
Each char of String s1 is compared with a char of String s2 at position count,
if they match count increases: count++;
If count has the length of String 2 all chars matched and true is returned.
there are two problems i can see in that code
1 for (int i = 1; i < s1.length(); i++) you are starting from index 1 but string indexes starts from 0
2 if (s1.charAt(i) != s1.charAt(i - 1)) here you are comparing characters of same strings s1 in other loop also this is the case
please fix these first, then ask again
this could be what you are searching for
public class Solution {
public static boolean checkSequence(String s1, String s2) {
for(char c : s2.toCharArray()) {
if(!s1.contains(c+"")) {
return false;
}
int pos = s1.indexOf(c);
s1 = s1.substring(pos);
}
return true;
}
}
Your approach to solve this problem can be something like this :
Find the smaller string.
Initialise the pointer to starting position of smaller string.
Iterate over the larger string in for loop and keep checking if character is matching.
On match increase the counter of smaller pointer.
while iterating keep checking if smaller pointer has reached to end or not. If yes then return true.
Something like this :
public static boolean checkSequence(String s1, String s2)
{
String smallerString = s1.length()<=s2.length() ? s1 : s2;
String largerString = smallerString.equals(s2) ? s1 : s2;
int smallerStringPointer=0;
for(int i=0;i<largerString.length();i++){
if(smallerString.charAt(smallerStringPointer) == largerString.charAt(i)){
smallerStringPointer++;
}
if(smallerStringPointer == smallerString.length()){
return true;
}
}
return false;
}
public static boolean usingLoops(String str1, String str2) {
int index = -10;
int flag = 0;
for (int i = 0; i < str1.length(); i++) {
flag = 0;
for (int j = i; j < str2.length(); j++) {
if (str1.charAt(i) == str2.charAt(j)) {
if (j < index) {
return false;
}
index = j;
flag = 1;
break;
}
}
if (flag == 0)
return false;
}
return true;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String str1 = s.nextLine();
String str2 = s.nextLine();
// using loop to solve the problem
System.out.println(usingLoops(str1, str2));
s.close();
}
To shrink a string "abbcccbfgh" by removing consecutive k characters till no removal can be done.
e.g. for k=3 output for the above string will be "afgh".
Please note that K and string both are dynamic i.e provided by the user.
I wrote the below program but I couldn't complete it. Please help.
public class Test {
public static void main(String[] args) {
String str = "abbcccbfgh";
int k = 3;
String result = removeConsecutive(str, k);
System.out.print("result is " + result);
}
private static String removeConsecutive(String str, int k) {
String str1 = str + "";
String res = "";
int len = str.length();
char c1 = 0, c2 = 0;
int count = 0;
for (int i = 0; i < len - 1; i++) {
c1 = str.charAt(i);
c2 = str.charAt(i + 1);
if (c1 == c2) {
count++;
} else {
res = res + String.valueOf(c1);
count = 0;
}
if (count == k-1) {
//remove String
}
}
return res;
}
I suggest to do it with regex:
int l = 0;
do {
l = str.length();
str = str.replaceAll("(.)\\1{" + n + "}", "");
} while (l != str.length());
n = k - 1
(.)\1{2} means any character followed by n same characters. \1 means the same character as in group #1
Is it okey to have recursion ?
public class Test {
public static void main(String[] args) {
String str = "abbcccbfgh";
int k = 3;
String result = removeConsecutive(str, k);
System.out.print("result is " + result);
}
private static String removeConsecutive(String str, int k) {
String ret = str;
int len = str.length();
int count = 0;
char c1 = 0 ;
char c2 = 0;
char last = 0 ;
for (int i = 0; i < ret.length()-1; i++) {
last = c1 ;
c1 = str.charAt(i);
c2 = str.charAt(i + 1);
if (c1 == c2 ) {
if( count > 0 ) {
if( last == c1 ) {
count ++ ;
}
else {
count = 0;
}
}
else {
count++;
}
} else {
count = 0;
}
if (count == k-1) {
int start = ((i+1) - k) + 1 ;
String one = str.substring(0, start) ;
String two = str.substring(start+k);
String new1 = one + two ;
//recursion
ret = removeConsecutive(new1, k) ;
count = 0;
}
}
return ret;
}
}
You can do it with a stack. For each character ch in the string, push it to the stack, if there's 3 consecutive same characters, pop them all. In the end, convert the stack to a string. You can improve the program a little by using a special stack that remembers the number of occurrences of each element.
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class Reduce implements Function<String, String> {
private final int k;
public Reduce(final int k) {
if (k <= 0) {
throw new IllegalArgumentException();
}
this.k = k;
}
#Override
public String apply(final String s) {
Stack<Character> stack = new Stack<>();
for (Character ch : s.toCharArray()) {
stack.push(ch);
if (stack.topCount() == k) {
stack.pop();
}
}
return stack.toString();
}
public static void main(String[] args) {
Reduce reduce = new Reduce(3);
System.out.println(reduce.apply("abbcccbfgh"));
}
private static class Stack<T> {
private class Node {
private T value;
private int count;
Node(T value) {
this.value = value;
this.count = 1;
}
}
private List<Node> nodes = new ArrayList<>();
public void push(T value) {
if (nodes.isEmpty() || !top().value.equals(value)) {
nodes.add(new Node(value));
} else {
top().count++;
}
}
public int topCount() {
return top().count;
}
public void pop() {
nodes.remove(nodes.size()-1);
}
private Node top() {
return nodes.get(nodes.size()-1);
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
nodes.forEach(n->{
for (int i = 0; i < n.count; i++) {
sb.append(n.value);
}
});
return sb.toString();
}
}
}
I know this problem is probably best served with DP, but I was wondering if it was possible to do it with recursion as a brute force way.
Given a set of words, say {"sales", "person", "salesperson"}, determine which words are compound (that is, it is the combination of 2 or more words in the list). So in this case, salesperson = sales + person, and is compound.
I based my answer heavily off of this problem: http://www.geeksforgeeks.org/dynamic-programming-set-32-word-break-problem/
public static void main(String args[]) throws Exception {
String[] test = { "salesperson", "sales", "person" };
String[] output = simpleWords(test);
for (int i = 0; i < output.length; i++)
System.out.println(output[i]);
}
static String[] simpleWords(String[] words) {
if (words == null || words.length == 0)
return null;
ArrayList<String> simpleWords = new ArrayList<String>();
for (int i = 0; i < words.length; i++) {
String word = words[i];
Boolean isCompoundWord = breakWords(words, word);
if (!isCompoundWord)
simpleWords.add(word);
}
String[] retVal = new String[simpleWords.size()];
for (int i = 0; i < simpleWords.size(); i++)
retVal[i] = simpleWords.get(i);
return retVal;
}
static boolean breakWords(String[] words, String word) {
int size = word.length();
if (size == 0 ) return true;
for (int j = 1; j <= size; j++) {
if (compareWords(words, word.substring(0, j)) && breakWords(words, word.substring(j, word.length()))) {
return true;
}
}
return false;
}
static boolean compareWords(String[] words, String word) {
for (int i = 0; i < words.length; i++) {
if (words[i].equals(word))
return true;
}
return false;
}
The problem here is now that while it successfully identifies salesperson as a compound word, it will also identify sales and person as a compound word. Can this code be revised so that this recursive solution works? I'm having trouble coming up with how I can easily do this.
Here is a solution with recursivity
public static String[] simpleWords(String[] data) {
List<String> list = new ArrayList<>();
for (String word : data) {
if (!isCompound(data, word)) {
list.add(word);
}
}
return list.toArray(new String[list.size()]);
}
public static boolean isCompound(String[] data, String word) {
return isCompound(data, word, 0);
}
public static boolean isCompound(String[] data, String word, int iteration) {
if (data == null || word == null || word.trim().isEmpty()) {
return false;
}
for (String str : data) {
if (str.equals(word) && iteration > 0) {
return true;
}
if (word.startsWith(str)) {
String subword = word.substring(str.length());
if (isCompound(data, subword, iteration + 1)) {
return true;
}
}
}
return false;
}
Just call it like this:
String[] data = {"sales", "person", "salesperson"};
System.out.println(Arrays.asList(simpleWords(data)));