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 4 years ago.
Improve this question
I am trying to calculate the common string using lcs, but this algorithm only calculates 1 string. What can I use instead?
LCS = "aaabbbcccxxx" and "aaadddccc" result: "aaa"
but what i want= "aaaccc"
help please:)
You can apply your LCS algorithm once to get the "aaa" result, then remove this result from both strings, and re-apply your LCS algorithm to get the "ccc" result. Finally you will concatenate the temporary results.
Your java code in the main class may look like the following (assuming that you have a method LCS(String string_1 ,String string_2) performing yourLCS algorithm:`
public static ArrayList<String> temp_results;
public static String string_1,string_2,temp_result,final_string;
public static void main(String args[]) {
while (temp_result != null && !temp_result.equals("")) {
temp_result = LCS(string_1,string_2);
string_1.replaceAll(temp_result,"");
string_2.replaceAll(temp_result,"");
temp_results.add(temp_result);
}
for (String iterator_string : temp_results){
final_string = final_string + iterator_string;
}
System.out.println("This is the result "+final_string);
}
public static String LCS(String string_1, String string_2){
return ""; //put your actual LCS logic here, you should not return an empty string!
}
Related
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 6 months ago.
Improve this question
I have a string statement String s = sb.contains("is");
I want to use this in an if condition like
if (s) {
//do something
}
How can I achieve this functionality?
Edit: Basically I get a boolean logic along with strings as an input. Ex: "Stack & over & (is | flow)". I have an array of sentences and I have to pick every sentence that follows this logic. I thought I would construct a string like "sb.contains(stack) && sb.contains(over) && (sb.contains(is) || sb.contains(flow))" and I thought I would run this boolean logic over all the sentences. Is there any other way of doing this?
The contains method returns a boolean value, so you can just simply set the type of variable 's' to a boolean.
public class Main
{
public static void main(String[] args) {
String sb = "crisis";
boolean s = sb.contains("is");
if(s){
System.out.println("S is true");
System.out.println(s);
}
}
}
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence)
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;
}
}
}
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 6 years ago.
Improve this question
In the following example, this program snippet does not compile, but if we replace the method Character.toString(char) by String.valueOf(char), everything is fine. What is the issue why Character.toString here ? In the documentation, these methods seem to have the same behavior. Thanks for explanation.
public static void main (String args[]) {
String source = "19/03/2016 16:34";
String result = Character.toString(source.substring(1,3));
System.out.print(result);
}
Character.toString(char c) method accepts char value as an argument and you are passing a String class instance which is produced from source.substring(1,3) method. String and char are incompatible types, so compiler can't create correct method call and pass the value
Your code should be rewritten as:
public static void main (String args[]) {
String source = "19/03/2016 16:34";
String result = source.substring(1, 3);
System.out.print(result);
//equivalent to the previous System.out.println call
System.out.print(source.substring(1, 3));
}
Also note that the first substring argument is an inclusive start index, the second one is exclusive end index and the leading index in Java String is 0 (not 1) exactly like in arrays (which is not a coincidence - String characters are stored in char array). So if you want to get a "19" String you should write source.substring(0, 2)
What does the compiler error message say? Anyway, source.substring(1,3) gives you a String while Character.toString() needs a char and does not accept a String.
String.valueOf(source.substring(1,3)) would call String.valueOf(Object), not String.valueOf(char).
You may obtain the same even simpler:
String result = source.substring(1,3);
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 7 years ago.
Improve this question
I have this task:
Write a class with Guttery name specifier and the public
which contain visible public static method called getPalterly
and the arguments dhangar, puccinoid type string and
which returns string. The method checks getPalterly
if dhangar string is part of puccinoid and if
the above proposal is verified returns the first 3
characters string dhangar. If the above link does not
verified then returns as a string the length of the string
dhangar.
I have written this code:
public class Guttery {
public static String getPalterly(String dhangar , String puccinoid) {
int g = dhangar.length();
if (puccinoid.contains(dhangar)) {
return dhangar.substring(g - 3 , g);
} else {
int a = dhangar.length();
String b = Integer.toString(a);
return b;
}
}
}
But i have this error:
The result returned by getPalterly method is wrong. (hint: do not return the correct string )
Any ideas?
Did you write you should return the first 3 characters ?
Then :
if (puccinoid.contains(dhangar)) {
return dhangar.substring(0 , 3);
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 8 years ago.
Improve this question
It is asked in an interview to write the code in Java to display the string which doesn't have consecutive repeated characters.
E.g.: Google, Apple, Amazon;
It should display "Amazon"
I wrote code to find continues repeating char. Is there any algorithm or efficient way to find it?
class replace
{
public static void main(String args[])
{
String arr[]=new String[3];
arr[0]="Google";
arr[1]="Apple";
arr[2]="Amazon";
for(int i=0;i<arr.length;i++)
{
int j;
for(j=1;j<arr[i].length();j++)
{
if(arr[i].charAt(j) == arr[i].charAt(j-1))
{
break;
}
}
if(j==arr[i].length())
System.out.println(arr[i]);
}
}
}
Logic : Match the characters in a String with the previous character.
If you find string[i]==string[i-1]. Break the loop. Choose the next string.
If you have reached till the end of the string with no match having continuous repeated character, then print the string.