I have opened an account for Ridit, one of 7-years-old students learning Java at SPOJ. The first task i gave to him was PALIN -The Next Palindrome. Here is the link to this problem- PALIN- The next Palindrome- SPOJAfter i explained it to him, he was able to solve it mostly except removing the leading zeros, which i did. Following is his solution of the problem -
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Scanner in = new Scanner(System.in);
int t = Integer.parseInt(in.nextLine());
String[] numbersInString = new String[t];
for (int i = 0; i <t; i++) {
String str = in.nextLine();
numbersInString[i] = removeLeadingZeros(str);
}
for (int i = 0 ; i<t; i++) {
int K = Integer.parseInt(numbersInString[i]);
int answer = findTheNextPalindrome(K);
System.out.println(answer);
}
}catch(Exception e) {
return;
}
}
static boolean isPalindrome(int x) {
String str = Integer.toString(x);
int length = str.length();
StringBuffer strBuff = new StringBuffer();
for(int i = length - 1;i>=0;i--) {
char ch = str.charAt(i);
strBuff.append(ch);
}
String str1 = strBuff.toString();
if(str.equals(str1)) {
return true;
}
return false;
}
static int findTheNextPalindrome(int K) {
for(int i = K+1;i<9999999; i++) {
if(isPalindrome(i) == true) {
return i;
}
}
return -1;
}
static String removeLeadingZeros(String str) {
String retString = str;
if(str.charAt(0) != '0') {
return retString;
}
return removeLeadingZeros(str.substring(1));
}
}
It is giving correct answer in Eclipse on his computer, but it is failing in SPOJ. If someone helps this little boy in his first submission, it will definitely make him very happy. I couldn't find any problem with this solution... Thank you in advance...
This might be helpful
import java.io.IOException;
import java.util.Scanner;
public class ThenNextPallindrom2 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int t = 0;
Scanner sc = new Scanner(System.in);
if(sc.hasNextInt()) {
t = sc.nextInt();
}
sc.nextLine();
int[] arr, arr2;
while(t > 0) {
t--;
String s = sc.nextLine();
arr = getStringToNumArray(s);
if(all9(arr)) {
arr2 = new int[arr.length + 1];
arr2[0] = 1;
for(int i=0;i<arr.length;i++) {
arr2[i+1] = 0;
}
arr2[arr2.length -1] = 1;
arr = arr2;
} else{
int mid = arr.length/ 2;
int left = mid-1;
int right = arr.length % 2 == 1 ? mid + 1 : mid;
boolean left_small = false;
while(left >= 0 && arr[left] == arr[right]) {
left--;
right++;
}
if(left < 0 || arr[left] < arr[right]) left_small = true;
if(!left_small) {
while(left >= 0) {
arr[right++] = arr[left--];
}
} else {
mid = arr.length/ 2;
left = mid-1;
int carry = 1;
if(arr.length % 2 == 0) {
right = mid;
} else {
arr[mid] += carry;
carry = arr[mid]/10;
arr[mid] %= 10;
right = mid + 1;
}
while(left >= 0) {
arr[left] += carry;
carry = arr[left] / 10;
arr[left] %= 10;
arr[right++] = arr[left--];
}
}
}
printArray(arr);
}
}
public static boolean all9(int[] arr) {
for(int i=0;i<arr.length;i++) {
if(arr[i] != 9)return false;
}
return true;
}
public static void printArray(int[] arr) {
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]);
}
System.out.println();
}
public static int[] getStringToNumArray(String s) {
int[] arr = new int[s.length()];
for(int i=0; i<s.length();i++) {
arr[i] = Integer.parseInt(String.valueOf(s.charAt(i)));
}
return arr;
}
}
I am having a problem where when I am trying to calculate a scientific expression, it compiles however, it crashes when I run it. The way my program works is that it receives a string containing a mathematical expression which needs to be evaluated. Firstly, it is split into an ArrayList containing numbers and operators separately. It is then searched for any brackets which allows the program to work with the expressions within the brackets first. Then it calls calculate which calculates the answer. The error I keep receiving however is " Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsExcepton: Index:0, Size:0" I assume it has something to do with my sizes of the arraylist however, I cannot spot the error, can anyone else help me figure it out please? I believe the issue is most likely with the methods splitLabel and calculate
public ArrayList<String> splitLabel(String val){
ArrayList<String> label_split = new ArrayList<String>();
String value = "";
String op = "";
for (int i = 0; i< val.length(); i++){
boolean isDigit = Character.toString(val.charAt(i)).matches("[0123456789.]+");
boolean isOperator = Character.toString(val.charAt(i)).matches("[+*/^-]+");
boolean isSin = (Character.toString(val.charAt(i)).equals("s") && Character.toString(val.charAt(i+1)).equals("i") && Character.toString(val.charAt(i+2)).equals("n"));
boolean isCos = (Character.toString(val.charAt(i)).equals("c") && Character.toString(val.charAt(i+1)).equals("o") && Character.toString(val.charAt(i+2)).equals("s"));
boolean isTan = (Character.toString(val.charAt(i)).equals("t") && Character.toString(val.charAt(i+1)).equals("a") && Character.toString(val.charAt(i+2)).equals("n"));
boolean isOpBracket = Character.toString(val.charAt(i)).equals("(");
boolean isClBracket = Character.toString(val.charAt(i)).equals(")");
if (isOperator && !value.equals("")){
op = Character.toString(val.charAt(i));
label_split.add(value);
label_split.add(op);
op = "";
value = "";
} else if (isOperator && value.equals("")){
if (Character.toString(val.charAt(i)).equals("-")){
value = Character.toString(val.charAt(i));
}
} else if (isSin){
label_split.add("sin");
}else if (isCos){
label_split.add("cos");
}else if (isTan){
label_split.add("tan");
} else if (isOpBracket && !value.equals("")){
label_split.add(value);
label_split.add("(");
value = "";
} else if (isOpBracket && value.equals("")){
label_split.add("(");
} else if (isClBracket && !value.equals("")){
label_split.add(value);
label_split.add(")");
value = "";
}
if (i== val.length()-1 && !value.equals("")){
label_split.add(value);
} else if (i== val.length()-1 && Character.toString(val.charAt(i)).equals(")")){
label_split.add(")");
}
} return label_split;
}
public String bracket(ArrayList<String> label_split){
ArrayList<Integer> opBra = new ArrayList<Integer>();
ArrayList<Integer> clBra = new ArrayList<Integer>();
ArrayList<String> calculation = new ArrayList<String>();
int counter = 0;
int counter1 = 0;
if (label_split.contains("(") && label_split.contains(")")){
for (int j=0; j<label_split.size(); j++){
if (label_split.get(j).equals("(")){
counter = counter + 1;
opBra.add(j);
} else if (label_split.get(j).equals(")")){
counter1 = counter1 + 1;
clBra.add(j);
}
}
if (counter1 != counter){
return "error missing bracket";
} else {
for (int j=opBra.size(); j>0; j--){
int opBraPos = opBra.get(j) + 1; //+1 and -1 so it doesn't include ()
int clBraPos = clBra.get(opBra.size()-j) - 1;
opBra.remove(j);
clBra.remove(opBra.size()-j);
for(int t = 0; t < (clBraPos - opBraPos); t++){
calculation.add(label_split.get(t+opBraPos));
}
String value = calculate(calculation);
label_split.set(j , value);
calculation.clear();
for (int n = 0; n < ((clBraPos+1) - opBraPos); n++){
label_split.remove(n);
}
}
}
return calculate(label_split);
} else{
return calculate(label_split);
}
}
public String calculate(ArrayList<String> calculation){
double value = 0.0;
String value1 = "";
boolean isOperator = calculation.contains("[+*/^-]+");
boolean isSin = calculation.contains("sin"); //...ETC
for (int i=0; i < calculation.size(); i++){
if (calculation.get(i).equals("^") && i < calculation.size() && i < 0){
boolean isDigit1 = calculation.get(i-1).matches("[0123456789.-]+");
boolean isDigit2 = calculation.get(i+1).matches("[0123456789.-]+");
if (isDigit1 && isDigit2){
value = Math.pow(Double.parseDouble(calculation.get(i-1)), Double.parseDouble(calculation.get(i+1)));
value1 = Double.toString(value);
calculation.set(i,value1);
calculation.remove(i-1);
calculation.remove(i+1);
}
}
}
for (int a=0; a < calculation.size(); a++){
if ( (calculation.get(a)).equals("sin") && a < calculation.size() && a < 0){
boolean isDigit1 = calculation.get(a+1).matches("[0123456789.-]+");
if (isDigit1){
value = Math.sin(Double.parseDouble(calculation.get(a+1)));
value1 = Double.toString(value);
calculation.set(a,value1);
calculation.remove(a+1);
}
}
}
for (int b=0; b < calculation.size(); b++){
if ( (calculation.get(b)).equals("cos") && b < calculation.size() && b < 0){
boolean isDigit1 = calculation.get(b+1).matches("[0123456789.-]+");
if (isDigit1){
value = Math.cos(Double.parseDouble(calculation.get(b+1)));
value1 = Double.toString(value);
calculation.set(b,value1);
calculation.remove(b+1);
}
}
} // ETC
return calculation.get(0);
}
This is not your problem, but it certainly seems wrong:
calculation.set(i,value1);
calculation.remove(i-1);
calculation.remove(i+1);
The problem is that remove(i-1) is going to cause the index of everything from i onwards to be decremented by one. Then remove(i+1) is going to remove the element that was previously at index i+2.
Then there is this:
for (int i=0; i < calculation.size(); i++){
if (calculation.get(i).equals("^") &&
i < calculation.size() &&
i < 0) {
Think about it. If i starts at zero and is incremented up to size() which is non-negative, how can i ever be less than zero. And if i is never less than zero, how can the if test ever succeed. (You repeat this pattern in other places.)
However, the real problem is that somehow you are calling calculate on an empty list.
I think it is time that you learned to use a debugger .....
I am suppose to use Boolean to check if the string is palindrome. I'm getting an error, not sure what I am doing wrong. My program already has 3 strings previously imputed by a user. Thank you, I am also using java
public boolean isPalindrome(String word1, String word2, String word3){
int word1Length = word1.length();
int word2Length = word2.length();
int word3Length = word3.length();
for (int i = 0; i < word1Length / 2; i++)
{
if (word1.charAt(i) != word1.charAt(word1Length – 1 – i))
{
return false;
}
}
return isPalindrome(word1);
}
for (int i = 0; i < word2Length / 2; i++)
{
if (word2.charAt(i) != word2.charAt(word2Length – 1 – i))
{
return false;
}
}
return isPalindrome(word2);
}
for (int i = 0; i < word3Length / 2; i++)
{
if (word3.charAt(i) != word3.charAt(word3Length – 1 – i))
{
return false;
}
}
return isPalindrome(word3);
}
// my output should be this
if (isPalindrome(word1)) {
System.out.println(word1 + " is a palindrome!");
}
if (isPalindrome(word2)) {
System.out.println(word2 + " is a palindrome!");
}
if (isPalindrome(word3)) {
System.out.println(word3 + " is a palindrome!");
}
You could do a method for it like this:
First you build a new String and than you check if it is equal.
private static boolean test(String word) {
String newWord = new String();
//first build a new String reversed from original
for (int i = word.length() -1; i >= 0; i--) {
newWord += word.charAt(i);
}
//check if it is equal and return
if(word.equals(newWord))
return true;
return false;
}
//You can call it several times
test("malam"); //sure it's true
test("hello"); //sure it's false
test("bob"); //sure its true
public class ProblemFour {
public static void main(String[] args) {
boolean externalCheck = true;
boolean internalCheck = true;
String Str = new String();
int prod = 0;
int mod = 0;
while (externalCheck == true) {
for (int a = 999; a > 99; a--) {
for (int b = 999; b > 99; b--) {
prod = a * b;
Str = Integer.toString(c);
if (internalCheck == true && mod < Str.length() / 2) {
if (c % 2 == 0) {
for (int i = 0; i < Str.length(); i++) {
if (Str.substring(i) != Str.substring(Str.length() - 1)) {
internalCheck = false;
} else {
mod++;
}
} else {
String S = Str.substring(0, (Str.length() - 1) / 2) + Str.substring(Str.length() / 2, Str.length());
for (int i = 0; i < S.length(); i++) {
if (S.substring(i) != Str.substring(S.length() - 1)) {
internalCheck = false;
} else {
mod++;
}
}
}
} else {
return Str;
System.out.println(Str);
externalCheck = false;
}
I am getting an error that says I have an else without and if and I don't understand where this is coming from. Specifically on line 31. What more detail do i need. is this not enough detail
You're missing four closing braces: One just before:
else{
String S= Str.substring(0,(Str.length()-1)/2) + Str.substring(Str.length()/2, Str.length());
And three at the end of your method.
Or maybe those aren't the right places... but they make everything balance. You have other problems after that.
I would strongly recommend that you learn how to use an editor that can assist you with writing Java code - e.g. the Eclipse IDE or Java Mode in Emacs, or whatever. Trying to track down issues like this when the code looks like yours does is torture. A good development editor will help you keep your braces all lined up.
You don't have a closing brace for if (c%2 ==0):
if (c%2 ==0) {
for(int i =0; i<Str.length(); i++) {
if(Str.substring(i) != Str.substring(Str.length()-1)) {
internalCheck = false;
} else {
mod++;
}
}
} else { //You are missing the } here. What you have is just "else {"
...
}
I'm trying to implement String method contains() without using the built-in contains() method.
Here is what I have so far:
public static boolean containsCS(String str, CharSequence cs) {
char[] chs = str.toCharArray();
int i=0,j=chs.length-1,k=0,l=cs.length();
//String str = "Hello Java";
// 0123456789
//CharSequence cs = "llo";
while(i<j) {
if(str.charAt(i)!=cs.charAt(k)) {
i++;
}
if(str.charAt(i)==cs.charAt(k)) {
}
}
return false;
}
I was just practicing my algorithm skills and got stuck.
Any advice?
Using Only 1 Loop
I did some addition to Poran answer and It works totally fine:
public static boolean contains(String main, String Substring) {
boolean flag=false;
if(main==null && main.trim().equals("")) {
return flag;
}
if(Substring==null) {
return flag;
}
char fullstring[]=main.toCharArray();
char sub[]=Substring.toCharArray();
int counter=0;
if(sub.length==0) {
flag=true;
return flag;
}
for(int i=0;i<fullstring.length;i++) {
if(fullstring[i]==sub[counter]) {
counter++;
} else {
counter=0;
}
if(counter==sub.length) {
flag=true;
return flag;
}
}
return flag;
}
This should work fine..I am printing execution to help understand the process.
public static boolean isSubstring(String original, String str){
int counter = 0, oLength = original.length(), sLength = str.length();
char[] orgArray = original.toCharArray(), sArray = str.toCharArray();
for(int i = 0 ; i < oLength; i++){
System.out.println("counter at start of loop " + counter);
System.out.println(String.format("comparing %s with %s", orgArray[i], sArray[counter]));
if(orgArray[i] == sArray[counter]){
counter++;
System.out.println("incrementing counter " + counter);
}else{
//Special case where the character preceding the i'th character is duplicate
if(counter > 0){
i -= counter;
}
counter = 0;
System.out.println("resetting counter " + counter);
}
if(counter == sLength){
return true;
}
}
return false;
}
Hints:
Use a nested loop.
Extracting the chars to an array is probably a bad idea. But if you are going to do it, you ought to use it!
Ignore the suggestion to use fast string search algorithms. They are only fast for large scale searches. (If you look at the code for String.indexOf, it just does a simple search ...)
As JB Nizet suggested, here is the actual code for contains():
2123 public boolean contains(CharSequence s) {
2124 return indexOf(s.toString()) > -1;
2125 }
And here is the code for indexOf():
1732 public int indexOf(String str) {
1733 return indexOf(str, 0);
1734 }
Which leads to:
1752 public int indexOf(String str, int fromIndex) {
1753 return indexOf(value, offset, count,
1754 str.value, str.offset, str.count, fromIndex);
1755 }
Which finally leads to:
1770 static int indexOf(char[] source, int sourceOffset, int sourceCount,
1771 char[] target, int targetOffset, int targetCount,
1772 int fromIndex) {
1773 if (fromIndex >= sourceCount) {
1774 return (targetCount == 0 ? sourceCount : -1);
1775 }
1776 if (fromIndex < 0) {
1777 fromIndex = 0;
1778 }
1779 if (targetCount == 0) {
1780 return fromIndex;
1781 }
1782
1783 char first = target[targetOffset];
1784 int max = sourceOffset + (sourceCount - targetCount);
1785
1786 for (int i = sourceOffset + fromIndex; i <= max; i++) {
1787 /* Look for first character. */
1788 if (source[i] != first) {
1789 while (++i <= max && source[i] != first);
1790 }
1791
1792 /* Found first character, now look at the rest of v2 */
1793 if (i <= max) {
1794 int j = i + 1;
1795 int end = j + targetCount - 1;
1796 for (int k = targetOffset + 1; j < end && source[j] ==
1797 target[k]; j++, k++);
1798
1799 if (j == end) {
1800 /* Found whole string. */
1801 return i - sourceOffset;
1802 }
1803 }
1804 }
1805 return -1;
1806 }
I came up with this:
public static boolean isSubString(String s1, String s2) {
if (s1.length() > s2.length())
return false;
int count = 0;
//Loop until count matches needle length (indicating match) or until we exhaust haystack
for (int j = 0; j < s2.length() && count < s1.length(); ++j) {
if (s1.charAt(count) == s2.charAt(j)) {
++count;
}
else {
//Redo iteration to handle adjacent duplicate char case
if (count > 0)
--j;
//Reset counter
count = 0;
}
}
return (count == s1.length());
}
I have recently stumbled upon this problem, and though I would share an alternative solution. I generate all the sub strings with length of the string we looking for, then push them into a hash set and check if that contains it.
static boolean contains(String a, String b) {
if(a.equalsIgnoreCase(b)) {
return true;
}
Set<String> allSubStrings = new HashSet<>();
int length = b.length();
for(int i=0; i<a.length(); ++i) {
if(i+length <= a.length()) {
String sub = a.substring(i, i + length);
allSubStrings.add(sub);
}
}
return allSubStrings.contains(b);
}
public static boolean contains(String large, String small) {
char[] largeArr = large.toCharArray();
char[] smallArr = small.toCharArray();
if (smallArr.length > largeArr.length)
return false;
for(int i = 0 ; i <= largeArr.length - smallArr.length ; i++) {
boolean result = true ;
for(int j = 0 ; j < smallArr.length ; j++) {
if(largeArr[i+j] != smallArr[j]) {
result = false;
break;
}
result = result && (largeArr[i+j]==smallArr[j]);
}
if(result==true) {return true;}
}
return false;
}
Certainly not the most efficient solution due to the nested loop, but it seems to work pretty well.
private static boolean contains(String s1, String s2) {
if (s1.equals(s2)) return true;
if (s2.length() > s1.length()) return false;
boolean found = false;
for (int i = 0; i < s1.length() - s2.length(); i++) {
found = true;
for (int k = 0; k < s2.length(); k++)
if (i + k < s1.length() && s1.charAt(i + k) != s2.charAt(k)) {
found = false;
break;
}
if (found) return true;
}
return false;
}
It can be done using a single loop.
public boolean StringContains(String full, String part) {
long st = System.currentTimeMillis();
if(full == null || full.trim().equals("")){
return false;
}
if(part == null ){
return false;
}
char[] fullChars = full.toCharArray();
char[] partChars = part.toCharArray();
int fs = fullChars.length;
int ps = partChars.length;
int psi = 0;
if(ps == 0) return true;
for(int i=0; i< fs-1; i++){
if(fullChars[i] == partChars[psi]){
psi++; //Once you encounter the first match, start increasing the counter
}
if(psi == ps) return true;
}
long et = System.currentTimeMillis()- st;
System.out.println("StringContains time taken =" + et);
return false;
}