Find all substring contain a keyword [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to find all the substring of one String that contains a key word.
Ex: "This is the keyword in the string".
Output: the keyword, this is the keyword, the keyword in the string, is the keyword in ....
I am think of finding all the substrings first then try to filter one by one. But I think that would be very bad solution.
Could you please give me some advice to do that!. Thank you very much.
I have edited to just find the sequence of tokens.

Try this:
String str = "abcdefkeybncv...";
String key = "key";
int index = str.indexOf(key);
ArrayList<String> sub = new ArrayList<String>();
for (int i = 0; i < str.length(); i++) {
for (int j = 0; j <= str.length() - i; j++) {
String s = str.substring(i, i+j);
if(s.indexOf(key) >= 0){
sub.add(s);
}
}
}
System.out.println(sub);
Output for the code above:
[abcdefkey, abcdefkeyb, abcdefkeybn, abcdefkeybnc, abcdefkeybncv, abcdefkeybncv., abcdefkeybncv.., abcdefkeybncv..., bcdefkey, bcdefkeyb, bcdefkeybn, bcdefkeybnc, bcdefkeybncv, bcdefkeybncv., bcdefkeybncv.., bcdefkeybncv..., cdefkey, cdefkeyb, cdefkeybn, cdefkeybnc, cdefkeybncv, cdefkeybncv., cdefkeybncv.., cdefkeybncv..., defkey, defkeyb, defkeybn, defkeybnc, defkeybncv, defkeybncv., defkeybncv.., defkeybncv..., efkey, efkeyb, efkeybn, efkeybnc, efkeybncv, efkeybncv., efkeybncv.., efkeybncv..., fkey, fkeyb, fkeybn, fkeybnc, fkeybncv, fkeybncv., fkeybncv.., fkeybncv..., key, keyb, keybn, keybnc, keybncv, keybncv., keybncv.., keybncv...]

Build suffix array: http://en.wikipedia.org/wiki/Suffix_array
Use binary search to find your substring there
Move up and down from this point in suffix array while suffixes starts with substring

Related

How to split a string at every Nth occurrence of a character in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to split a String at every Nth occurence, but missing the last values.Here is what is expected.
Input : String str = "234-236-456-567-678-675-453-564";
Output :
234-236-456
567-678-675
453-564
Here N=3, where the str should be split at every 3rd occurence of -.
Try this.
String str = "234-236-456-567-678-675-453-564";
String[] f = str.split("(?<=\\G.*-.*-.*)-");
System.out.println(Arrays.toString(f));
result:
[234-236-456, 567-678-675, 453-564]
You can try the following with Java 8:
String str = "234-236-456-567-678-675-453-564";
Lists.partition(Lists.newArrayList(str.split("-")), 3)
.stream().map(strings -> strings.stream().collect(Collectors.joining("-")))
.forEach(System.out::println);
Output:
234-236-456
567-678-675
453-564
Maybe one of the worst way without using function available in java , but good like exercise :
public static void main(String[] args){
String s = "234-236-456-567-678-675-453-564";
int nth =0;
int cont =0;
int i=0;
for(;i<s.length();i++){
if(s.charAt(i)=='-')
nth++;
if(nth == 3 || i==s.length()-1){
if(i==s.length()-1) //with this if you preveent to cut the last number
System.out.println(s.substring(cont,i+1));
else
System.out.println(s.substring(cont,i));
nth=0;
cont =i+1;
}
}
}

What is the difference between a String[] and String in a for loop (Java) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Before you ask, yes I did Google this first. I haven't found a proper answer yet. I understand the syntax for a String array in a for loop, but not for a String. For example, let's say I have a fragment code which includes a for loop that's purpose is to adjust an element of the String to "josh" if that element isn't equal to something (I can't think of anything at the top of my head). The fragment code would be like this:
public void adjustScore(String[] str){
for(int j= 0; j < str.length; j++){
if(str[j] != //idk, something//
str[j]= "josh";
}
else{};
}
But, how would this look if it was a String instead of a String[]?
public void adjustScore(String str2){
for(int j=0; j < str2.length(); j++){
// How do I call an element from the String? Would I still use str2[j]?//
In Java, a String is not an array of characters. Although it is true that the only "elements" in a String are characters, you can use String.charAt(int) to get a character at a valid index (but you cannot use []).
char ch = str2.charAt(j);

Store all possible substring in String [] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to store all possible substring in String []. I tried this but got an error:
public void sub(String word){
String [] Str=new String[100];
int n=0;
for (int from = 0; from < word.length(); from++) {
for (int to = from + 1; to <= word.length(); to++) {
str[n]=word.substring(from, to);
n++;
System.out.println(str[n]);
}
}
}
What is solution?
error is: cannot find symbol, variable str, loction: class substring
Well, that fairly clearly tells you what the error is: You haven't declared str. You have declared Str, but Java's identifiers are case sensitive, str and Str are not the same identifier.
So change
String [] Str=new String[100];
to
String [] str=new String[100];
// ^--- lower case
Before, when you hadn't said what the error was, there were a couple of other things Pshemo and I (amongst others) noticed:
You have a sequence issue here:
str[n]=word.substring(from, to);
n++;
System.out.println(str[n]);
...since you're incrementing n before outputting the string, you're always going to output null. Just moving the increment fixes that:
str[n]=word.substring(from, to);
System.out.println(str[n]);
n++;
Another possible problem can occur for longer words, where number of substrings can be more then 100. In that case you should avoid creating fixed size array, but try using dynamic size collection like List
List<String> str = new ArrayList<String>();
To put or read elements here just use str.add(substring) and str.get(index).

Reverse and repeat String words in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
So I am having trouble repeating my text 3 times, reversing the sentence and counting the letters's in the word. Can someone please help me out? I have tried googling and looking for a method and cannot seem to get any of them to work. It would be greatly appreciated
You have a misplaced semi-colon at the end of your if-statement meaning that the statement after it will always be executed.
if(text.charAt(i) == 'e'); // Remove the last character here
{
System.out.println("e :" + text.charAt(i) + i);
count++;
}
As for your reverseLetters function, I don't know if the return text; outside of your for loop can be reached but you're overwriting the value of reverse on each iteration. I think what you should be trying instead is to append the value of text.charAt(j) to the value of reverse like so:
String reverse = ""; // Must be initialised to an empty string
for(int j = text.length()-1; j >= 0; j--)
{
reverse += text.charAt(j);
/* The rest of the contents of your loop here */
And what do you expect your repeatLetters function to do? You're assigning the given String to a local variable named repeat and then you're just returning that value without doing anything. You could use a loop to append text to an empty String three times and return that.
String repeat = "";
for (int i = 0; i < 3; i++)
{
repeat += text + " ";
}
return repeat;

Counted and Terminated String in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Two methods of implementing a string. A counted string explicitly
records its length. The terminated string’s length is determined by an
end-of-string mark.
Can anyone give an example of counted string and a terminated string in java.
CountedString {
char[] string;
int length;
int getLength() {
return length;
}
}
TerminatedString {
char[] string;
final static char TERMINATOR = '$';
int getLength() {
for (int i = 0; i < string.length; i++) {
if (string[i] == TERMINATOR) return i;
}
}
}
If you look in to the String.java will find that the length of String is being calculated by counter which traverse through string's characters .
Please referString.java for more information. You should look in to this class to see implementation of length() method.

Categories

Resources