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 10 months ago.
Improve this question
Why doesn't this solution work for finding valid anagram?
26/36 test cases get passed in LeetCode.
class Solution {
public boolean isAnagram(String s, String t) {
int sASCII = 0, tASCII = 0;
if(s.length() != t.length()) {return false;}
else{
for(int i = 0 ; i < s.length(); i++){
sASCII += (int)s.charAt(i);
tASCII += (int)t.charAt(i);
}
}
if(sASCII == tASCII){
return true;
}
return false;
}
}
The sums tASCII and sASCII can be equal even if the numbers are not anagrams. Let's say that you can get the number 100 by adding 60+40, but you can also get it by adding 70+30, so i recommend to use a HashMap to note every occurence of every letter or to sort the strings as arrays of chars and then compare them.
Related
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 last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I've multiple List of Java-Datetimes with MyCustomDate object contains ( start DateTime and end DateTime ) LocalDateTime with hours and seconds.
I need to write function to check if There is Overlapping dates in my list return true \ false - with the best performance
can use java8 (stream functions)
You have to test all ranges aginst each other. There is no other way. Try this way, although is just one implementation, there are plenty of correct ways to implement it:
public boolean isThereOverlapingRanges(List<CustomDateRange> ranges) {
if (ranges.size() <= 1) {
return false;
}
boolean overlaping = false;
for(int i = 1; i < ranges.size(); i++) {
if (isDateInRange(ranges.get(0).start(), ranges.get(i)) ||
isDateInRange(ranges.get(0).end(), ranges.get(i))) {
overlaping = true;
break;
}
}
return overlaping || isThereOverlapingRanges(ranges.subList(1, ranges.size());
}
private boolean isDateInRange(DateTime date, CustomDateRange range) {
return date.after(range.start()) && date.before(range.end());
}
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 1 year ago.
Improve this question
This code work properly. I need help to change this into int function. Please don't add extra int or void function to it.
public void binary(int n) {
if(n==0)
return;
binary(n>>1);
System.out.printf("%d",n%2);
}
Here is the changed code:
public int binary(int n)
{
if (n == 0 || n == 1) return n;
return binary(n >> 1) * 10 + n % 2;
}
It returns an int and it will return binary form of the number. This code will fail to provide correct output after the number 1023. The binary of 1023 is 1111111111.
After this, any number you enter will cause overflow issues and you will receive a wrong output. You can further increase the range of the program if you use long. However, that too will fail to provide any correct output after 524287 for the same reasons mentioned before.
You can use BigInteger class if you want to store binary of any numbers bigger than 524287.
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 3 years ago.
Improve this question
public static int countX(String str) {
if (str.length() == 0) {
return 0;
}
if (str.charAt(0) == 'x') {
return 1 + countX(str.substring(1));
} else {
return countX(str.substring(1));
}
}
Given an input String of "xxx", the method above shall return 3.
I understand the flow of the method, the line "return 1 + countX(str.substring(1));" adds one if an 'x' is found. What I don't understand is how does that return value carry over to the next iteration/recursion? I don't see the value of the increment stored anywhere.
Look at the line return 1 + countX(str.substring(1));
Now suppose that str was "xx", so the substring passed to the recursive call is "x".
So in that call, since the first character is 'x', it again executes
return 1 + countX(str.substring(1));
in the next recursive call, the substring is empty, so it returns zero to the previous call, which then returns (1+0) to its previous call, which returns ( 1 + (1 +0) ) to the initial call of the method, so the result for the String "xx" becomes 1+1+0 = 2.
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
This is homework. I get the logic but i got stuck on the code. I've done it with normally way and it takes 1 week to get the code. I need to get repeat string with recursive way in Java.
This is my code :
static String repeatString (final int n, final String[] syllables, final String currentWord) {
if (n == 0) {
System.out.println(currentWord);
} else {
for (int i = 0; i < syllables.length; i++) {
repeatString(n - 1, syllables, currentWord + syllables[i]);
}
}
return "";
}
if i call in main method like
String[] str = {"a", "b"};
repeatString(1, str, " ");
then i get output (a,b) if i change to
repeatString(2,str," ");
then i get output ( aa,ab,ba,bb) if i change to
repeatString(3,str," ");
then i get output (aaa,aab,aba,abb,baa,bab,bba,bbb) and so on.
So basically it is like 2 to the power to n. If n=1, i got 2, if n=3, i got 8, and so on.
I would be grateful if someone can help me to get this code in recursive way.
Any help is much appreciated.
The method you have there is recursive already. Being recursive does NOT mean it should have no for loops. A recursive method in cheap words means the method calls itself, which yours does.
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 am making a game four in a row, where the number of rows and colomns is determined by the player(so can be anything, as long as more than four). So users input is stored in the variables "rows" and "column". The field looks smth like this, so the first row is always numbers.
I have trouble with the search algorithm for the winner.
While my algorithm for the horizontal search works fine, the vertical one with the same logic gives me the out of bound error.If you could please help me to spot the mistake, I would be very grateful. Thank you!
1 2 3 4 5
|-|-|-|-|
|-|-|-|-|
|-|-|-|-|
public static String checkWinner(String [][]field){
//horizontally, which works
for(int i=1; i<=rows; i++){
for (int j=0; j<=column-1;j++){
if (((j>=3 || j==column-1) && field[i][j]!="|_" && field[i][j]==field[i][j-1] && field[i][j]==field[i][j-2] && field[i][j]==field[i][j-3])
|| (field[i][j]!="|_" && field[i][j]==field[i][j+1] && field[i][j]==field[i][j+2] && field[i][j]==field[i][j+3]))
{
return field[i][j];
}
}
}
//vertically which doesn't work
for(int i=0; i<column; i++){
for (int j=1; j<=rows-1;j++){
if (((j>=4 || j==rows-1) && field[j][i]!="|_" && field[j][i]==field[j-1][i] && field[j][i]==field[j-2][i] && field[j][i]==field[j-3][i])
|| (field[j][i]!="|_" && field[j][i]==field[j+1][i] && field[j][i]==field[j+2][i] && field[j][i]==field[j+3][i]))
{
return field[i][j];
}
}
}
return null;
}
In your Horizontally loop logic you are running your loop for column from j=0 to column-1 but then you are accessing field[i][j] with values like j+1, j+2, j+3 which will go beyond column
for example : user enters column value as 5 but your logic of j+2 at j=4 & i=0 will make it to access 6th column (field[0][6]) which is obviously OutOfBound.
check for same error in Vertically Loop logic for rows value
and as NOTE: compare strings using equals() not using equality operators like ==