I checked old topics with this problem and couldn't fix it.
This method is meant to compare two strings, and if every letter/character of the first string is found in the second string (not necessarily vice versa), then the method should return "true" (even if the second string has extra letters).
My idea is to check the letter at every index of the first string, see if it's in the second string, and if it is, delete that letter in both strings. When the first string runs out of letters (length equals zero) then the boolean should return true.
I think my loops or substring reaches out of range at some point.
public boolean isFound(String first, String second) {
StringBuilder sb1 = new StringBuilder(first);
StringBuilder sb2 = new StringBuilder(second);
first.toCharArray();
second.toCharArray();
for (int i = 0; i < first.length(); i++) {
int k = (first.substring(i, i + 1)).indexOf(second, i);
if (sb1.length() > 0) {
sb1.deleteCharAt(k);
sb2.deleteCharAt(k);
}
}
if (sb1.length() == 0) {
return true;
} else {
return false;
}
}
Ex: "at" and "tack" should return true, "tree" and "ere" should return false.
EDIT
After reviewing the comments, my new code is now this. It always returns false, though even with "tree" and "tree".
public boolean isFound(String first, String second){
StringBuilder sb2 = new StringBuilder(second);
for(int i=0;i<first.length();i++){
int k = sb2.indexOf(first,i);
if (k==-1)
return false;
else sb2.deleteCharAt(k);
}
return true;
}
You have a number of problems in your code.
You only need one StringBuilder version, that of second
The calls to toCharArray() are superfluous
You should not search for each character of first in second but in the mutable version of it sb2.
You are using indexOf wrong. This method should be called on the StringBuilder object to search for the first argument, you have it swapped.
The pseudocode that you can use is
isSuperAnagram(String first, String second) {
sb = StringBuilder(second)
for all chars in first {
k = search index of current char of first in sb
if(k == -1) // char is not in sb
return false
else
remove char at index k from sb
}
return true
}
please review your algorithm and code/usage of APIs
` first.toCharArray();`
second.toCharArray();
wont convert first,second to array, this API would return a character array.
(first.substring(i,i+1)).indexOf(second,i); will search the whole substring2 in first.substring.
review the algo/code correct this accordingly.please take care of all the edge/corner cases.
ideal method will be to use a hashmap.(on cost of extra space)
There is simple logic you can implement it your code
for(int i=0;i<sb1.length;i++)
{
for(int j=0;j<sb2.length;j++)
{
if(sb1.[i]==sb2.[j])
{
count++;
break;
}
if(count>=sb1.length)
{
System.out.print("match");
}
}
}
if you want more condition then post it below my post i wrote for only your follow example
Ex: "at" and "tack" should return true, "tree" and "ere" should return false.
Related
My goal is to return a string that is made up of characters that are 1 before and 1 after each character in the given string in the ASCII table.
For example, dog should return cenpfh.
public String encrypt(String s) {
if (s.length() == 1) {
String one = "";
one += (s.charAt(0)-1) + (s.charAt(0)+1);
return one;
} else { .. }
}
This is the code I have so far, and I have no idea how to continue. This doesn't even work for a single character, for example, "172" is returned for "V". Could anyone point me in the right direction? Really stumped right now.
If you have to use recursion, you can try something like that:
public static String encrypt(final String s) {
if (s.length() == 1) {
final char c = s.charAt(0);
return new String(new char[] {(char) (c - 1), (char) (c + 1)});
} else {
return encrypt(s.substring(0, 1)) + encrypt(s.substring(1));
}
}
the main idea here:
if input length is 1 symbol - we know what to do and just perform "encryption"
if input length is more than 1 symbol - we split string into "first symbol" and "rest of the string" and call itself on both parts
This is fairly tricky stuff for a beginner, but it's good stuff to encounter early and understand.
"" + 172
results in the string "172"
"" + 'V'
results in the string "V".
V and 172 are both represented internally as the same number, so what's the difference? The difference is that the compiler keeps track of the type of the value. 'V' has a type of char. 172 has a type of int.
When you perform an expression using a mixture of types, the compiler has to decide what the type of the result is. This will influence how that result is handled by routines such String concatenation.
As it happens when you subtract an int from a char, the type of the result is an int (the Java spec tells you this).
You can get around it by casting the type to something else.
(char) 172 is 'V'
(int) 'V' is 172.
So:
one += (char) (s.charAt(0)-1) + (char)(s.charAt(0)+1);
Recursion is a separate topic. Recursion works for handling sequences (a String is a sequence of characters) when you can do a chunk of work on a part of the sequence, leaving you with a new, shorter sequence, that you can do the same job on -- until finally you have the simplest case, which you simply return.
The simplest case, the one where the method returns without recursively calling itself, is called the terminating case. You need at least one terminating case, otherwise the recursive method will keep calling itself until Java runs out of memory for storing method states.
You have chosen your terminating case as being a one char string. I would instead pick a zero length string:
public void encrypt(String s) {
if("".equals(s)) {
return "";
} else {
// do something with a string that's guaranteed >1 char
}
Now you just have to work out how to replace that comment, by using up the guaranteed one char, and calling encrypt() to handle what's left.
public void encrypt(String s) {
if("".equals(s)) {
return "";
} else {
return encodeOneChar(s.charAt(0)) +
encrypt(x.substring(1));
}
}
That's it:
when called with an empty string, encode() returns an empty string.
when called with a non-empty string, encode processes the first char
and calls itself to deal with a string that's one shorter.
Some languages, like Haskell, make recursion easier to understand, by representing the cases as pattern matching statements.
encrypted "" == ""
encrypted s == encryptChar(head(s)) + encrypted(tail(s))
(that's not real Haskell, but it illustrates the pattern.)
Recursion isn't quite as easy to read in Java, but the pattern is still there. if(terminating case) { return answer for terminating case } else { calculate something, call self, return combined result }
I'll leave you to implement private String encodeOneChar(char c)
Note that recursion isn't a good solution to your actual task in Java -- except in the sense that your goal is to understand recursion.
There is no recursion needed. Just iterate over input chars and collect result in a StringBuilder:
public static String encrypt(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
sb.append((char)(c - 1)).append((char)(c + 1));
}
return sb.toString();
}
Note that explicit cast to char is required here, because s.charAt(i) - 1 evaluates to int and without cast is processed by overloaded append(int i) method.
Okay, recursion. Here I split input into 2 halves and process each by using the same encryptRecursively() method, then join results. Each part, when passed to the method, is again split into 2 parts, etc, until size becomes 0 or 1 (thus we get a very simple implementation of Divide and Conquer):
public static String encryptRecursively(String s) {
if (s.isEmpty())
return "";
else if (s.length() == 1) {
char c = s.charAt(0);
return new String(new char[] { (char)(c - 1), (char)(c + 1) });
} else {
int mid = s.length() / 2;
return encryptRecursively(s.substring(0, mid)) // process first half of input
+ encryptRecursively(s.substring(mid)); // second half
}
}
You don't need a recursion but a simple for-loop through all chars in the word. You get the value of char with s.charAt(...), but you have to convert it back to char for the desired resut. Append them to StringBuilder and return its String as the output of the method:
public static String encrypt(String s) {
StringBuilder sb = new StringBuilder();
for (int i=0; i<s.length(); i++) {
char a = (char)(s.charAt(i)-1);
char b = (char)(s.charAt(i)+1);
sb.append(a).append(b);
}
return sb.toString();
}
Here is the recursion method as demanded:
public static String encrypt(String s) {
if (s.length() == 1) {
StringBuilder sb = new StringBuilder();
char a = (char)(s.charAt(0)-1);
char b = (char)(s.charAt(0)+1);
return sb.append(a).append(b).toString();
} else {
return encrypt(s.substring(0, 1)) + encrypt(s.substring(1, s.length()));
}
You can try this:
one += (char)(((int)s.charAt(0))-1);
one += (char)(((int)s.charAt(0))+1);
Input:
V
Output:
UW
I have a String with value
String rest="bac";
I have another String with value
String str="baack";
If i use
str.contains(rest);
it returns false. But i want the output to be true. As "baack" contains all the letters from string rest
Is it possible to do so? With or without this method?
Unfortunately, there is no standard method doing this, as far as I know.
If what you want is to check that the second string contains at least once every character of the first string, then you can check each character one by one with the following test:
boolean result = true;
for (char c : test.toCharArray()) {
result &= str.indexOf(c) > -1;
}
return result;
Or alternatively:
for (char c : test.toCharArray()) {
if (str.indexOf(c) == -1) {
return false;
}
}
return true;
It might not be optimal, but it works and it is simple to read.
Since the order is not important, your question turns to be whether the first set of character contains the second set of character.
// Initial the sets
Set<char> bigSet = new HashSet<char>(Arrays.asList(str));
Set<char> smallSet = new HashSet<char>(Arrays.asList(rest));
for (char c : smallSet) {
if(!bigSet.contains(c)){
return false;
}
}
return true;
Here is another way to make sure all characters from one string are in the second string. It is a lengthy way but it is one of the very basic ways to work with characters from String in java. I know it is not optimal but it serves the purpose.
String rest="bac";
String str="baack";
char[] strChar = str.toCharArray();
char[] restChar = rest.toCharArray();
int count = 0;
for(int i=0;i<restChar.length;i++){
for(int j=0;j<strChar.length; j++){
if(restChar[i] == strChar[j]){
count++;
}
}
}
if(count>=restChar.length){
System.out.println("All the characters from: "+rest+" are in: "+str);
}
This is from cracking the Coding Interview Book
The Questions Implement an algorithm to determine if a string has all unique characters. What if
you can not use additional data structures?
I am wondering what is happening in the if statement below? can anyone explain it to me ?
I have left my understanding of the code in the comments.Please correct me if i am wrong
public class Uniquechar2 {
public static boolean isUniqueChars2(String str) {
// Create a new boolean array of 256 characters to account for basic a cii and extended ascii characters
boolean[] charSet = new boolean[256];
//iterate through the array
for (int i = 0; i < str.length(); i++) {
// Assign the value of current value of the iterator i to int variable val.So if we are looping through "hello" at i = 0 the int value of 'h' will be assigned to val.Is that correct?
int val = str.charAt(i);
// Continuing from the example of loping throughout the string "hello" the if statement will see if 'h' is in charSet and since it will be there it will return false /is that what is happening?
if (charSet[val]) {
return false;
}
// Is this the else statement? true will be assigned to charSet[h] in this case
charSet[val] = true;
}
// I dont understand why we are returning true at the end ?
return true;
}
public static boolean isUniqueChars2(String str) {
// Create a new boolean array of 256 characters to account for basic ascii and extended ascii characters
boolean[] char_set = new boolean[256];
// Iterate through the string we are testing
for (int i = 0; i < str.length(); i++) {
// Get the numerical (ascii) value of the character in the `str` at position `i`.
int val = str.charAt(i);
// If char_set[val] has been set, that means that this character was already present in the string. (so in string 'hello' this would be true for the second 'l')
if (char_set[val]) {
return false;
}
// If the character hasn't been encountered yet (otherwise we would have returned false above), then mark this particular character as present in the string
char_set[val] = true;
}
// If the function hasn't returned false after going through the entire string that means that each character is unique - thus returning true
return true;
}
Is this the else statement
No, otherwise there would be an else in the code. But in this case, else is unnecessary since, if char_set[val] is true, the execution of the method stops immediately, due to the return false; instruction.
I dont understand why we are returning true at the end ?
Because since no duplicate has been found, the method must return true to indicate that the string is composed of unique characters. If a duplicate had been found, the method would have returned already in
if (char_set[val]) {
return false;
}
I would just use regex, which requires only one line of code:
public static boolean isUniqueChars(String str) {
return str.matches("((.)(?!.*?\\2))*");
}
Breaking down the regex:
(.) captures every character
(?!.*?\\2) is a negative look ahead for a back reference to the captured group
Together, these mean "a character that does not reappear after itself"
(...)* around the above means 0-n of them
Altogether, it means "comprised of characters that do do reappear later in the string", ie unique characters.
One-line solution without any extra data structure:
str.chars().distinct().count() == (int)str.length();
I have to be able to input any two words as a string. Invoke a method that takes that string and returns the first word. Lastly display that word.
The method has to be a for loop method. I kind of know how to use substring, and I know how to return the first word by just using .substring(0,x) x being how long the first word is.
How can I make it so that no matter what phrase I use for the string, it will always return the first word? And please explain what you do, because this is my first year in a CS class. Thank you!
I have to be able to input any two words as a string
The zero, one, infinity design rule says there is no such thing as two. Lets design it to work with any number of words.
String words = "One two many lots"; // This will be our input
and then invoke and display the first word returned from the method,
So we need a method that takes a String and returns a String.
// Method that returns the first word
public static String firstWord(String input) {
return input.split(" ")[0]; // Create array of words and return the 0th word
}
static lets us call it from main without needing to create instances of anything. public lets us call it from another class if we want.
.split(" ") creates an array of Strings delimited at every space.
[0] indexes into that array and gives the first word since arrays in java are zero indexed (they start counting at 0).
and the method has to be a for loop method
Ah crap, then we have to do it the hard way.
// Method that returns the first word
public static String firstWord(String input) {
String result = ""; // Return empty string if no space found
for(int i = 0; i < input.length(); i++)
{
if(input.charAt(i) == ' ')
{
result = input.substring(0, i);
break; // because we're done
}
}
return result;
}
I kind of know how to use substring, and I know how to return the first word by just using .substring(0,x) x being how long the first word is.
There it is, using those methods you mentioned and the for loop. What more could you want?
But how can I make it so that no matter what phrase I use for the string, it will always return the first word?
Man you're picky :) OK fine:
// Method that returns the first word
public static String firstWord(String input) {
String result = input; // if no space found later, input is the first word
for(int i = 0; i < input.length(); i++)
{
if(input.charAt(i) == ' ')
{
result = input.substring(0, i);
break;
}
}
return result;
}
Put it all together it looks like this:
public class FirstWord {
public static void main(String[] args) throws Exception
{
String words = "One two many lots"; // This will be our input
System.out.println(firstWord(words));
}
// Method that returns the first word
public static String firstWord(String input) {
for(int i = 0; i < input.length(); i++)
{
if(input.charAt(i) == ' ')
{
return input.substring(0, i);
}
}
return input;
}
}
And it prints this:
One
Hey wait, you changed the firstWord method there.
Yeah I did. This style avoids the need for a result string. Multiple returns are frowned on by old programmers that never got used to garbage collected languages or using finally. They want one place to clean up their resources but this is java so we don't care. Which style you should use depends on your instructor.
And please explain what you do, because this is my first year in a CS class. Thank you!
What do I do? I post awesome! :)
Hope it helps.
String line = "Hello my name is...";
int spaceIndex = line.indexOf(" ");
String firstWord = line.subString(0, spaceIndex);
So, you can think of line as an array of chars. Therefore, line.indexOf(" ") gets the index of the space in the line variable. Then, the substring part uses that information to get all of the characters leading up to spaceIndex. So, if space index is 5, it will the substring method will return the indexes of 0,1,2,3,4. This is therefore going to return your first word.
The first word is probably the substring that comes before the first space. So write:
int x = input.indexOf(" ");
But what if there is no space? x will be equal to -1, so you'll need to adjust it to the very end of the input:
if (x==-1) { x = input.length(); }
Then use that in your substring method, just as you were planning. Now you just have to handle the case where input is the blank string "", since there is no first word in that case.
Since you did not specify the order and what you consider as a word, I'll assume that you want to check in given sentence, until the first space.
Simply do
int indexOfSpace = sentence.indexOf(" ");
firstWord = indexOfSpace == -1 ? sentence : sentence.substring(0, indexOfSpace);
Note that this will give an IndexOutOfBoundException if there is no space in the sentence.
An alternative would be
String sentences[] = sentence.split(" ");
String firstWord = sentence[0];
Of if you really need a loop,
String firstWord = sentence;
for(int i = 0; i < sentence.length(); i++)
{
if(sentence.charAt(i) == ' ')
{
sentence = firstWord.substring(0, i);
break;
}
}
You may get the position of the 'space' character in the input string using String.indexOf(String str) which returns the index of the first occurrence of the string in passed to the method.
E.g.:
int spaceIndex = input.indexOf(" ");
String firstWord = input.substring(0, spaceIndex);
Maybe this can help you figure out the solution to your problem. Most users on this site don't like doing homework for students, before you ask a question, make sure to go over your ISC book examples. They're really helpful.
String Str = new String("Welcome to Stackoverflow");
System.out.print("Return Value :" );
System.out.println(Str.substring(5) );
System.out.print("Return Value :" );
System.out.println(Str.substring(5, 10) );
I'm working on a program for Java on how to find a list of palindromes that are embedded in a word list file. I'm in an intro to Java class so any sort of help or guidance will be greatly appreciated!
Here is the code I have so far:
import java.util.Scanner;
import java.io.File;
class Palindromes {
public static void main(String[] args) throws Exception {
String pathname = "/users/abrick/resources/american-english-insane";
File dictionary = new File(pathname);
Scanner reader = new Scanner(dictionary);
while (reader.hasNext()) {
String word = reader.nextLine();
for (int i = 0; i > word.length(); i++) {
if (word.charAt(word.indexOf(i) == word.charAt(word.indexOf(i)) - 1) {
System.out.println(word);
}
}
}
}
}
There are 3 words that are 7 letters or longer in the list that I am importing.
You have a few ways to solve this problem.
A word is considered a palindrome if:
It can be read the same way backwards as forwards.
The first element is the same as the last element, up until we reach the middle.
Half of the word is the same as the other half, reversed.
A word of length 1 is trivially a palindrome.
Ultimately, your method isn't doing much of that. In fact, you're not doing any validation at all - you're only printing the word if the first and last character match.
Here's a proposal: Let's read each end of the String, and see if it's a palindrome. We have to take into account the case that it could potentially be empty, or be of length 1. We also want to get rid of any white space in the string, as that can cause errors on validation - we use replaceAll("\\s", "") to solve that.
public boolean isPalindrome(String theString) {
if(theString.length() == 0) {
throw new IllegalStateException("I wouldn't expect a word to be zero-length");
}
if(theString.length() == 1) {
return true;
} else {
char[] wordArr = theString.replaceAll("\\s", "").toLowerCase().toCharArray();
for(int i = 0, j = wordArr.length - 1; i < wordArr.length / 2; i++, j--) {
if(wordArr[i] != wordArr[j]) {
return false;
}
}
return true;
}
}
I'm assuming that you're reading in strings. Use string.toCharArray() to convert each string to a char[]. Iterate through the character array using a for loop as follows: on iteration 1, if the first character is equal to the last character, then proceed to the next iteration, else return false. On iteration 2, if the second character is equal to the second-to-last character then proceed to the next iteration, else return false. And so on, until you reach the middle of the string, at which point you return true. Be careful of off-by-one errors; some strings will have an even length, some will have an odd length.
If your palindrome checker is case insensitive, then use string.toLowerCase().toCharArray() to preprocess the character array.
You can use string.charAt(i) instead of string.toCharArray() in the for loop; in this case, if the palindrome checker is case insensitive then preprocess the string with string = string.toLowerCase()
Let's break the problem down: In the end, you are checking if the reverse of the word is equal to the word. I'm going to assume you have all of the words stored in an array called wordArray[].
I have some code for getting the reverse of the word (copied from here):
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return new StringBuffer(str).reverse().toString();
}
So, now we just need to call that on every word. So:
for(int count = 0; count<wordArray.length;count++) {
String currentWord = wordArray[count];
if(currentWord.equals(reverse(currentWord)) {
//it's a palendrome, do something
}
}
Since this is homework, i'll not supply you with code.
When i code, the first thing i do is take a step back and ask myself,
"what am i trying to get the computer to do that i would do myself?"
Ok, so you've got this huuuuge string. Probably something like this: "lkasjdfkajsdf adda aksdjfkasdjf ghhg kajsdfkajsdf oopoo"
etc..
A string's length will either be odd or even. So, first, check that.
The odd/even will be used to figure out how many letters to read in.
If the word is odd, read in ((length-1)/2) characters.
if even (length/2) characters.
Then, compare those characters to the last characters. Notice that you'll need to skip the middle character for an odd-lengthed string.
Instead of what you have above, which checks the 1st and 2nd, then 2nd and 3rd, then 3rd and fourth characters, check from the front and back inwards, like so.
while (reader.hasNext()) {
String word = reader.nextLine();
boolean checker = true;
for (int i = 0; i < word.length(); i++) {
if(word.length()<2){return;}
if (word.charAt(i) != word.charAt(word.length()-i) {
checker = false;
}
}
if(checker == true)
{System.out.println(word);}
}