suffix tree implementation issue - java

I am studying some Suffix tree implementation and here is one reference implementation, and question is how "indexes" (refer line 19) is used for class SuffixTreeNode? I am not sure if "indexes" is useful and I think probably we just need to keep all nodes and their children character value? Not find too much values of "indexes" is used for class SuffixTreeNode.
Please feel free to correct me. Any insights are appreciated.
public class SuffixTree {
SuffixTreeNode root = new SuffixTreeNode();
public SuffixTree(String s) {
for (int i = 0; i < s.length(); i++) {
String suffix = s.substring(i);
root.insertString(suffix, i);
}
}
public ArrayList<Integer> getIndexes(String s) {
return root.getIndexes(s);
}
}
public class SuffixTreeNode {
HashMap<Character, SuffixTreeNode> children = new
HashMap<Character, SuffixTreeNode>();
char value;
ArrayList<Integer> indexes = new ArrayList<Integer>();
public SuffixTreeNode() { }
public void insertString(String s, int index) {
indexes.add(index);
if (s != null && s.length() > 0) {
value = s.charAt(0);
SuffixTreeNode child = null;
if (children.containsKey(value)) {
child = children.get(value);
} else {
child = new SuffixTreeNode();
children.put(value, child);
}
String remainder = s.substring(1);
child.insertString(remainder, index);
}
}
public ArrayList<Integer> getIndexes(String s) {
if (s == null || s.length() == 0) {
return indexes;
} else {
char first = s.charAt(0);
if (children.containsKey(first)) {
String remainder = s.substring(1);
return children.get(first).getIndexes(remainder);
}
}
return null;
}
}
public class Question {
public static void main(String[] args) {
String testString = “mississippi”;
String[] stringList = {“is”, “sip”, “hi”, “sis”};
SuffixTree tree = new SuffixTree(testString);
for (String s : stringList) {
ArrayList<Integer> list = tree.getIndexes(s);
if (list != null) {
System.out.println(s + “: “ + list.toString());
}
}
}
}

indexes is surely needed for the implementation you are looking at of Suffix Tree (there are multiple versions of suffix tree some more optimized than others). The indexes variable plays an integral part in returning the indices where the sub-string (is, sip, hi, sis) exist in the original string (mississippi) back to the calling method. getIndexes returns indexes in its base case this is how you get the list of occurrences of each sub-string. see below output
is: [1, 4]
sip: [6]
sis: [3]

Related

Suggestions on how to fix this tricky problem in Java

In the given array in Java, [766-09-9090, 766-09-9090, 877-90-9090, 877-90-9090, "S", "T", "U"]
How could we obtain a new array with values like this :
[766-09-9090, 877-90-9090, 877-90-9090, 766-90-9090, "S", "T", "U"]
Note : No changes on non SSN values like "S", "T, "U"
This is my first stab but I am not getting the results I am looking for. Any suggestion would be appreciate
public static modifyArray(List<String> arrays) {
List<String> newArray = new ArrayList<String>();
boolean matchedFound = false;
for (int index = 0; index < arrays.size(); index++) {
if (arrays.get(index).length() == 9 && isValidSSN(arrays.get(index))) {
String nextMatchingSsn = getNextDistinctSsn(arrays);
System.out.println("Next Distinct SSN IS : " + nextMatchingSsn);
if (nextMatchingSsn != "") {
String[] pair = nextMatchingSsn.split(":");
if (pair.length == 2) {
Integer key = Integer.parseInt(pair[1]);
String ssn = pair[0];
swap(arrays.toArray(), index, key);
}
}
newArray.add(nextMatchingSsn);
} else {
System.out.println("Non Matching " + arrays.get(index));
newArray.add(arrays.get(index));
}
}
}
private static boolean isValidSSN(String s) {
if (s.length() != 9) {
throw new IllegalArgumentException("An SSN length must be 9");
}
for (int i = 0; i < 9; i++)
if (!Character.isDigit(s.charAt(i))) {
throw new IllegalArgumentException("SSN must have only digits.");
}
return (true);
}
private static String getNextDistinctSsn(List<String> ssns) {
String firstDiffSsn = "";
String currentSsn = "";
for (int index = 0; index < ssns.size(); index++) {
if (!currentSsn.equals(ssns.get(index)) && currentSsn != "") {
firstDiffSsn = ssns.get(index);
return firstDiffSsn + ":" + index;
} else {
currentSsn = ssns.get(index);
}
}
return firstDiffSsn;`enter code here`
}
public static final <T> void swap (T[] a, int i, int j) {
T t = a[i];
a[i] = a[j];
a[j] = t;
}
This is my first stab but I am not getting the results I am looking for. Any suggestion would be appreciated. So basically, if I have to write a unit test my expected result would look something like this:
public void validateResult(){
}
I can see a number of problems.
Your code is creating a new list (called newArray !?!) and populating it, but then not using it.
Your code is splitting SSNs on a : character, but the input data has no : characters.
Your SSN validation method will throw an unchecked exception if it encounters something that is not a valid SSN, but your code expects it to return false in that scenario.
This is wrong: currentSsn != "". Do not use == or != to compare strings. You are liable to get an incorrect result.
And so on.

Given a Morse String with out any spaces, how to find the no. of words it can represent irrespective of the meaning

Given A morse String eg. aet = ".- . -" if the spaces are removed it will become an ambiguous morse string ".-.-" which can represent "aet","eta","ent","etet" etc.
the problem is to find the no.of words that the morse string without spaces can represent irrespective of the meaning of the words. The constraint is that the new word which is formed should be the same size of the input i.e "aet" = "ent" and other words like "etet" should be discarded.
i implemented a recursive solution for some reason it is not working. below is my code and thinking of converting this to DP approach to increase time efficiency. Can some one help to point out the mistake in the below code and is DP a right approach to follow for this problem? Thanks in advance!!
EDIT 1 :- The program gives me an output but not the correct one. for ex. for the morse String representing aet = ".- . -" if given without any spaces to the program ".-.-" it should give an out put "3" i.e 3 words can be formed that is of the same size as the input including the input "aet","eta","ent" but it gives me an output "1". I think there is some thing wrong with the recursive calls.
The approach used here is to simply cut the morse string in a place where first valid morse code is encountered and the repeat the process with the rest of the string untill 3 such valid morse code are found and check whether whole morse string is consumed. if consumed increment the word count and repeat the process for different values of substring size(end variable in the below code).
I hope this helps!!.Tried my best to explain as clearly as I could.
import java.util.*;
import java.io.*;
import java.math.*;
import java.text.*;
public class MorseCode2 {
static Map<String,String> morseCode;
static Map<String,String> morseCode2;
static int count = 0;
public static void main(String args[]){
String[] alpha = {"a","b","c","d","e","f","g","h","i","j","k",
"l","m","n","o","p","q","r","s","t","u","v",
"w","x","y","z"};
String[] morse = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",
".--","-..-","-.--","--.."};
morseCode = new HashMap<String,String>();
morseCode2 = new HashMap<String,String>();
for(int i = 0;i<26;i++){
morseCode.put(morse[i],alpha[i]);
}
for(int i = 0;i<26;i++){
morseCode2.put(alpha[i],morse[i]);
}
Scanner in = new Scanner(System.in);
String input = in.next();
String morseString = "";
for(int j = 0; j< input.length(); j++){
morseString += morseCode2.get(input.charAt(j)+"");
}
countPossibleWord(morseString,input.length(),0,1,0);
System.out.println(count);
in.close();
}
public static void countPossibleWord(String s,int inputSize,int start,int end,int tempCount){
if(start >= s.length() || end > s.length()){
return;
}
if(tempCount>inputSize){
return;
}
String sub = s.substring(start, end);
if(sub.length()>4){
return;
}
if(morseCode.get(sub)!=null){
tempCount++;
countPossibleWord(s,inputSize,end,end+1,tempCount);
}
else{
countPossibleWord(s,inputSize,start,end+1,tempCount);
}
if(tempCount == inputSize && end == s.length()){
count++;
}
countPossibleWord(s,inputSize,start,end+1,0);
}
}
EDIT 2 :- Thank you all for your Responses and Extremely sorry for the confusing code, will surely try to improve on writing neat and clear code. learnt a lot from your replies!!
And i also some how made the code work, the problem was I passed wrong argument which changed the state of the recursive calls. Instead of passing "tempCount-1" for the last argument in the last function call in the method "countPossibleWord" i passed "0" this altered the state. found this after running through the code manually for larger inputs. below is the corrected method
public static void countPossibleWord(String s,int inputSize,int start,int end,int tempCount){
if(start >= s.length() || end > s.length()){
return;
}
if(tempCount>inputSize){
return;
}
String sub = s.substring(start, end);
if(sub.length()>4){
return;
}
if(morseCode.get(sub)!=null){
tempCount++;
countPossibleWord(s,inputSize,end,end+1,tempCount);
}
else{
countPossibleWord(s,inputSize,start,end+1,tempCount);
}
if(tempCount == inputSize && end == s.length()){
count++;
}
countPossibleWord(s,inputSize,start,end+1,tempCount-1);
}
}
If you like to have a recursive function, you should be clear about your parameters (use as few as possible) as well as when to step down and when to go up again.
My solution would look something like
public static int countPossibleWord(String strMorse, String strAlpha, int inputSize) {
if (strMorse.length() > 0) { // still input to process
if (strAlpha.length() >= inputSize)
return 0; // String already has wrong size
int count = 0;
for (int i = 0; i < morse.length; i++) { // try all morse codes
if (strMorse.startsWith(morse[i])) { // on the beginning of the given string
count += countPossibleWord(strMorse.substring(morse[i].length()), strAlpha+alpha[i], inputSize);
}
}
return count;
} else {
if( strAlpha.length() == inputSize ) {
System.out.println( strAlpha );
return 1; // one solution has been found
} else {
return 0; // String has wrong size
}
}
}
Your morse and alpha arrays need to be static variables for this to work.
Note that there is only one situation where the recursion will step down: when there is some input left and the size limit is not reached. Then it will check for the next possible letter in the loop.
All other cases will lead the recursion to go one step up again - and when going up, it will return the number of solutions found.
Call it like this:
System.out.println(countPossibleWord(morseString, "", input.length() ));
The fact that you use a class variable instead of the returned value of the recursive function makes it extremely unclear. Even for you as #Thomas Weller said. You should clarify the possible cases when a count one more letter. I deleted eclipse, hence I coded it in C, I hope I will still help you to understand the algo :(understand char* as string)
char morse[26][5] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",
".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
int countPossibleWord(char* s, int inputSize, int start, char* buffer, int sizeBuff){
if(start == inputSize){
if(sizeBuff == 0) return 1;
else return 0;
}
char buff[sizeBuff+2]; //
strncpy(buff, buffer, sizeBuff);//
buff[sizeBuff] = s[start]; // buff = buff+s[start]
buff[sizeBuff+1] = '\0'; //
for(int i = 0; i < 26; ++i){
//run the equivalent of your map to find a match
if(strcmp(buff, morse[i]) == 0)
return countPossibleWord(s, inputSize, start+1, "", 0) + countPossibleWord(s, inputSize, start+1, buff, sizeBuff+1);
}
return countPossibleWord(s, inputSize, start+1, buff, sizeBuff+1);
}
The problem with your code is, that you don't understand it any more, because it's not clean as described by Robert C. Martin. Compare your code to the following. This is certainly still not the cleanest, but I think you can understand what it does. Tell me if you don't.
Consider this main program:
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
String morsetext = enterTextOnConsole();
MorseTable morseTable = new MorseTable();
MorseCode code = convertToMorseCodeWithoutSpaces(morsetext, morseTable);
List<String> guesses = getAllPossibleMeanings(code, morseTable);
List<String> guessesOfSameLength = filterForSameLength(morsetext, guesses);
printListOnConsole(guessesOfSameLength);
}
private static void printListOnConsole(List<String> guessesOfSameLength) {
for (String text : guessesOfSameLength) {
System.out.println(text);
}
}
private static List<String> filterForSameLength(String morsetext, List<String> guesses) {
List<String> guessesOfSameLength = new LinkedList<String>();
for (String guess : guesses) {
if (guess.length() == morsetext.length())
{
guessesOfSameLength.add(guess);
}
}
return guessesOfSameLength;
}
private static List<String> getAllPossibleMeanings(MorseCode code, MorseTable morseTable) {
MorseCodeGuesser guesser = new MorseCodeGuesser(morseTable);
List<String> guesses = guesser.guess(code);
return guesses;
}
private static MorseCode convertToMorseCodeWithoutSpaces(String morsetext, MorseTable morseTable) {
MorseCode code = new MorseCode(morseTable);
code.fromText(morsetext);
code.stripSpaces();
return code;
}
private static String enterTextOnConsole() {
Scanner scanner = new Scanner(System.in);
String text = scanner.next();
scanner.close();
return text;
}
}
and the following MorseTable class:
import java.util.HashMap;
import java.util.Map;
public class MorseTable {
private static final Map<String, String> morseTable;
private static int longestCode = -1;
static
{
morseTable = new HashMap<String, String>();
morseTable.put("a", ".-");
morseTable.put("b", "-...");
morseTable.put("c", "-.-.");
morseTable.put("e", ".");
morseTable.put("t", "-");
morseTable.put("n", "-.");
// TODO: add more codes
for (String code : morseTable.values()) {
longestCode = Math.max(longestCode, code.length());
}
}
public String getMorseCodeForCharacter(char c) throws IllegalArgumentException {
String characterString = ""+c;
if (morseTable.containsKey(characterString)) {
return morseTable.get(characterString);
}
else {
throw new IllegalArgumentException("No morse code for '"+characterString+"'.");
}
}
public int lengthOfLongestMorseCode() {
return longestCode;
}
public String getTextForMorseCode(String morseCode) throws IllegalArgumentException {
for (String key : morseTable.keySet()) {
if (morseTable.get(key).equals(morseCode)) {
return key;
}
}
throw new IllegalArgumentException("No character for morse code '"+morseCode+"'.");
}
}
and the MorseCode class
public class MorseCode {
public MorseCode(MorseTable morseTable)
{
_morseTable = morseTable;
}
final MorseTable _morseTable;
String morseCode = "";
public void fromText(String morsetext) {
for(int i=0; i<morsetext.length(); i++) {
char morseCharacter = morsetext.charAt(i);
morseCode += _morseTable.getMorseCodeForCharacter((morseCharacter));
morseCode += " "; // pause between characters
}
}
public void stripSpaces() {
morseCode = morseCode.replaceAll(" ", "");
}
public MorseCode substring(int begin, int end) {
MorseCode subcode = new MorseCode(_morseTable);
try{
subcode.morseCode = morseCode.substring(begin, end);
} catch(StringIndexOutOfBoundsException s) {
subcode.morseCode = "";
}
return subcode;
}
public MorseCode substring(int begin) {
return substring(begin, morseCode.length());
}
public String asPrintableString() {
return morseCode;
}
public boolean isEmpty() {
return morseCode.isEmpty();
}
}
and last not least, the MorseCodeGuesser
import java.util.LinkedList;
import java.util.List;
public class MorseCodeGuesser {
private final MorseTable _morseTable;
public MorseCodeGuesser(MorseTable morseTable) {
_morseTable = morseTable;
}
public List<String> guess(MorseCode code) {
List<String> wordList = new LinkedList<String>();
if (code.isEmpty()) return wordList;
for(int firstCodeLength=1; firstCodeLength<=_morseTable.lengthOfLongestMorseCode(); firstCodeLength++) {
List<String> guesses = guess(code, firstCodeLength);
wordList.addAll(guesses);
}
return wordList;
}
private List<String> guess(MorseCode code, int firstCodeLength) {
MorseCode firstCode = code.substring(0, firstCodeLength);
String firstCharacter;
try{
firstCharacter = _morseTable.getTextForMorseCode(firstCode.asPrintableString());
} catch(IllegalArgumentException i) {
return new LinkedList<String>(); // no results for invalid code
}
MorseCode remainingCode = code.substring(firstCodeLength);
if (remainingCode.isEmpty()) {
List<String> result = new LinkedList<String>();
result.add(firstCharacter); // sole result if nothing is left
return result;
}
List<String> result = new LinkedList<String>();
List<String> remainingPossibilities = guess(remainingCode);
for (String possibility : remainingPossibilities) {
result.add(firstCharacter + possibility); // combined results
}
return result;
}
}
I have pasted my own solution to it. I have followed DFS and it is giving the correct answer for the given problem statement. Please ask if there are any queries.
alpha =["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
key = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--",
"-..-","-.--","--.."]
dic = dict(list(zip(key,alpha)))
def morse_code(morse,count,res,char,length):
global dic
if count == length - 1:
if morse[char:] in dic:
res = res + 1
return res
word = ''
for i in range(char,len(morse)):
word = word + morse[i]
if word not in dic:
continue
else:
count = count + 1
res = morse_code(morse,count,res,i+1,length)
count = count - 1
return res
if __name__ = 'main'
inp = input()
morse = ''
for i in inp:
morse = morse + key[ord(i)-ord('a')]
result = morse_code(morse,0,0,0,len(inp))
print(result)

Ordering java linked list alphabetically (dictionary-like)

I've been working for hours trying to order a linked list of strings alphabetically (dictionary-like). The given string is lowercase only.
For example, input of: "hello my name is albert" will be sorted in the list as: Node 1: albert,
Node 2: hello,
Node 3: is,
etc..
My code so far reads a string like the example above and insert it as nodes - unordered.
I've searched in the web for ways to sort a linked list alphabetically with good performance, and I found Merge Sort can be usefull.
I've changed the merge sort to work for string using compareTo() but my code returns nullPointerException error in the following line:
if(firstList._word.compareTo(secondList._word) < 0){
I'm looking for help to fix the following code or another way for sorting a linked list alphabetically (without Collection.sort)
My full code is (after trying to add the merge sort to work with my code):
public class TextList
{
public WordNode _head;
public TextList()
{
_head = null;
}
public TextList (String text)
{
this._head = new WordNode();
int lastIndex = 0;
boolean foundSpace = false;
String newString;
WordNode prev,next;
if (text.length() == 0) {
this._head._word = null;
this._head._next = null;
}
else {
for (int i=0;i<text.length();i++)
{
if (text.charAt(i) == ' ') {
newString = text.substring(lastIndex,i);
insertNode(newString);
// Update indexes
lastIndex = i;
// set to true when the string has a space
foundSpace = true;
}
}
if (!foundSpace) {
//If we didnt find any space, set the given word
_head.setWord(text);
_head.setNext(null);
}
else {
//Insert last word
String lastString = text.substring(lastIndex,text.length());
WordNode lastNode = new WordNode(_head._word,_head._next);
_head.setNext(new WordNode(lastString,lastNode));
}
sortList(_head);
}
}
private void insertNode(String word)
{
//Create a new node and put the curret node in it
WordNode newWord = new WordNode(_head._word,_head.getNext());
//Set the new information in the head
_head._word = word;
_head.setNext(newWord);
}
private WordNode sortList(WordNode start) {
if (start == null || start._next == null) return start;
WordNode fast = start;
WordNode slow = start;
// get in middle of the list :
while (fast._next!= null && fast._next._next !=null){
slow = slow._next; fast = fast._next._next;
}
fast = slow._next;
slow._next=null;
return mergeSortedList(sortList(start),sortList(fast));
}
private WordNode mergeSortedList(WordNode firstList,WordNode secondList){
WordNode returnNode = new WordNode("",null);
WordNode trackingPointer = returnNode;
while(firstList!=null && secondList!=null){
if(firstList._word.compareTo(secondList._word) < 0){
trackingPointer._next = firstList; firstList=firstList._next;
}
else {
trackingPointer._next = secondList; secondList=secondList._next
;}
trackingPointer = trackingPointer._next;
}
if (firstList!=null) trackingPointer._next = firstList;
else if (secondList!=null) trackingPointer._next = secondList;
return returnNode._next;
}
public String toString() {
String result = "";
while(_head.getNext() != null){
_head = _head.getNext();
result += _head._word + ", ";
}
return "List: " + result;
}
public static void main(String[] args) {
TextList str = new TextList("a b c d e a b");
System.out.println(str.toString());
}
}
In the past i have made a method to sort strings alphabetically in an array as school HW, so umm here it is:
private void sortStringsAlphabetically(){
for (int all = 0; all < names.length; all++) {
for (int i = all + 1; i < names.length; i++) {
if (names[all].compareTo(names[i]) > 0) {
String tmp = names[i];
names[i] = names[all];
names[all] = tmp;
}
}
}
}
This piece of code works for Arrays and specifically for an array of names. You can tweak it to work with the list, it is very simple especially if we consider the wide range of methods in the List interface and all it's implementations.
Cheers.
If you don't wanna to have a huge code who gets every first letter of the word and sort them, do it with Collection.sort()
I don't know what is the proplem on Collection.sort() so use it
Here is a short code, that does exactually this what you want to:
String test = "hello my name is albert";
test = test.replaceAll(" ", "\n");
String[] te = test.split("\n");
List<String> stlist = new ArrayList<String>();
for(String st : te) {
stlist.add(st);
}
Collections.sort(stlist);
Regarding NPE you said it is probably because you are having an null string in head at first and keep adding this in insert method.
this._head = new WordNode();
Also the adding last element is also not proper. Just reuse the insert method like below
insertNode(text.substring(lastIndex,text.length()));
These are the ones I thought having problem when you are converting string to lined list
You can use the below code to handle the first null
private void insertNode(String word) {
if (this._head == null) {
this._head = new WordNode(word, null);
} else {
WordNode newWord = new WordNode(_head._word, _head.getNext());
_head._word = word;
_head.setNext(newWord);
}
}

Adding a sentence word by word into a set using Recursion

I am trying to add each word of a sentence into a set using recursion in java. Punctuation does not matter.
My problem is that only the first word of the sentence is being printed after I print the list.
For example the sentence "One Two Three Four" would come out in my list as just [One].
public static TreeSet<String> getWordSet(String words) {
TreeSet<String> result = new TreeSet<String>();
int index = words.indexOf(" ");
if (index < 0) {
return result;
} else {
result.add(words.substring(0, index));
getWordSet(words.substring(index + 1));
}
return result;
}
Is there something I'm missing or overlooking?
You should add result of returned value of your recursion function to your result set,(also you didnt consider the last word), something like this(I give explanation in the comments)
public static TreeSet<String> getWordSet(String words) {
TreeSet<String> result = new TreeSet<String>();
int index = words.indexOf(" ");
if (index < 0 && words.length() == 0) {
return result;
}else if (index < 0 && words.length() > 0) { // here you didnt consider the last word
result.add(words);
} else {
result = getWordSet(words.substring(index + 1)); //here we first get result of recursion then add our new value to the list
result.add(words.substring(0, index));
}
return result;
}
Take care of the scopes. Your TreeSet is a local variable, It is getting over-written every time you call the function with a new one.
Try declaring it as a global variable outside the function.
Here's the recursive solution based on your original implementation.
import java.util.TreeSet;
public class RecursiveSplit {
public static TreeSet<String> getWordSet(String sentence, TreeSet<String> mySet) {
int index = sentence.indexOf(" ");
if (index < 0)
if (sentence.length()>0)
index = sentence.length() - 1;
else
return mySet;
mySet.add(sentence.substring(0, index));
getWordSet(sentence.substring(index+1), mySet);
return mySet;
}
public static TreeSet<String> getWordSetDriver(String sentence){
TreeSet<String> blankSet = new TreeSet<String>();
return getWordSet(sentence, blankSet);
}
public static void main(String[] args) {
for (String s : getWordSetDriver("This is a sentence.")) {
System.out.println(s);
}
}
}
Solves the problem recursively.. Well I don't know why you intend to do that, but this is not the best way.
private static TreeSet<String> result = new TreeSet<String>();
public static TreeSet<String> getWordSet(String words) {
int index = words.indexOf(" ");
if (index < 0 && words != null) {
return result;
} else if (index < 0 && words.length() > 0) {
result.add(words);
} else {
result = getWordSet(words.substring(index + 1));
result.add(words.substring(0, index));
}
return result;
}
I'll only address your dynamic programming as an exercise here, note that this kind of solution is not a good approach for solving your task.
In each invocation of your method, you are instantiating a new Set, which you will eventually throw away, and just return the very first one, which contains the first word. You will need to create the collection object outside the recursive method, then pass it around by reference, like this:
public static void main (String[] args) throws java.lang.Exception
{
Set<String> set = new TreeSet<String>();
getWordSet("get word set", set);
System.out.println(set.toString());
}
public static void getWordSet(String words, Set set) {
int index = words.indexOf(" ");
if (index < 0) {
if (words.length() > 0) set.add(words);
} else {
set.add(words.substring(0, index));
getWordSet(words.substring(index + 1), set);
}
}
The line if (words.length() > 0) set.add(words); will add the last word, in case the input string does not end with a space.
Demo here: http://ideone.com/ruEMjA

Need to delete string from arraylist which contains letters in it and don't touch if contains other

So I'm trying to make a method fix to remove all words which contain letter r in it,then multiply x2 all words which contain letter l in it and if word contains r and l in it don't touch it. Method works but not how it must. I think it have to do something with remove.
public class Solution
{
public static void main(String[] args) throws Exception
{
BufferedReader bis = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> list = new ArrayList<String>();
list.add("rose"); //0
list.add("liar"); //1
list.add("lisa"); //2
list = fix(list);
for (String s : list)
{
System.out.println(s);
}
}
public static ArrayList<String> fix(ArrayList<String> list) {
//
ArrayList<String> temp = new ArrayList<String>();
for(int i = 0; i < list.size(); i++){
String s = list.get(i);
boolean strL = s.contains("l");
boolean strR = s.contains("r");
if(strR){
temp.remove(s);
}
if(strL && strR) {
temp.add(s);
} else {
if(strL) {
temp.add(s);
temp.add(s);
}
}
}
return temp;
}
}
You should use the Iterator,
and the exclusive or (^) operator.
public static ArrayList<String> fix(ArrayList<String> list) {
ArrayList<String> temp = new ArrayList<>();
for (Iterator<String> i = list.iterator(); i.hasNext();) {
String str = i.next();
boolean isL = str.contains("l");
boolean isR = str.contains("r");
if (isR ^ isL) {
if (isR) {
i.remove();
}
else {
temp.add(str);
temp.add(str);
}
}
else {
temp.add(str);
}
}
return temp;
}
I think your logic should be:
if (strL != strR) {
list.remove(word);
}
If it contains neither, it does nothing. If it contains both, it does nothing. If it contains L and not R, it removes it. If it contains R and not L, it removes it.
Of course my code is modifying the input list. If you want to build up a temp list instead, it should be easy to extrapolate my answer to get what you want.
Try this
if(strL && strR) {
temp.add(s);
} else if(strL) {
// do nothing
} else if(strR) {
// do nothing
} else {
temp.add(s);
}
Or simple you can do
if(strL && strR) {
temp.add(s);
} else (!strL && !strR) {
temp.add(s);
}
Hopefully it will work
You don't have to remove from the temp list.
The only thing to do is to test the value that you get at each iteration from the list you passed in parameter:
check if both conditions are true, in this case just add the original word.
test if it contains a l, then multiply it by two
otherwise do nothing (you didn't precise what to do for words that doesn't contain l and r):
if(strL && strR) {
temp.add(s);
} else if(strL){
temp.add(s);
temp.add(s);
}
Which outputs:
liar
lisa
lisa
Here is the answer...
public class Solution {
public static void main(String[] args) throws Exception {
ArrayList<String> list = new ArrayList<String>();
list.add("rose"); // 0
list.add("love"); // 1
list.add("lyre"); // 2
list = fix(list);
for (String s : list) {
System.out.println(s);
}
}
public static ArrayList<String> fix(ArrayList<String> list) {
//
ArrayList<String> teampList = new ArrayList<String>();
for (int i = 0; i < list.size(); i++) {
String mainList = list.get(i);
boolean checkL = mainList.contains("l");
boolean checkR = mainList.contains("r");
if (checkR) {
teampList.remove(mainList);
}
if (checkL && checkR) {
teampList.add(mainList);
} else {
if (!checkL && !checkR) {
teampList.add(mainList);
}
if (checkL) {
teampList.add(mainList);
teampList.add(mainList);
}
}
}//end for
return teampList;
}
}

Categories