Substring a String depending on int [duplicate] - java

This question already has answers here:
Java: How to split a string by a number of characters?
(11 answers)
Closed 5 years ago.
I wanted to substring a String depending on int that I will passed on the method. I used nested loop for this. But everytime it loops I wanted to substring only from last substring to int the I passed in the method and get also the last string. How can I achieve this?
private static void input(String s, int I)
{
List list = new ArrayList();
for(int a = 0; a < s.length(); a++)
{
for(int position = 0; position < s.length(); position++)
{
if(position + a + I <= s.length())
{
list.add(s.substring(position, position + a + I));
}
}
}
}
input("abaca", 2);
Expected output: "ab", "ac", "a"

You don't need nested loops. Just iterate over the String once, and add an I character substring in each iteration.
Note that a is the starting index of the current substring, and it therefore incremented by I in each iteration.
The last substring may have a shorter length. If a + I > s.length(), the last index of the last substring will be s.length() - 1 instead of a + I - 1.
for(int a = 0; a < s.length(); a+=I) {
list.add(s.substring(a, Math.min(a + I, s.length())));
}
This produces
[ab, ac, a]
for input("abaca", 2).

You can also simply split the string:
private static void input(String s, int i){
List list = Arrays.asList(s.split("(?<=\\G.{"+i+"})"));
System.out.println(list);
}
\\G means The end of the previous match
?<= means positive lookbehind
Thanks to positive lookbehind (?<=) it will split on all zero length strings (without cutting off anything from the input string) preceded by where previous match ends (\\G) followed by i signs (.{"+i+"}).

It's really better to completely rewrite your code, but if you want to save your structure(even though it's not correct), just remove 1 loop and do something like this:
private static void input(String s, int I) {
List list = new ArrayList();
for (int position = 0; position < s.length();position += I) {
if (position + I <= s.length()) {
list.add(s.substring(position, position + I));
} else {
list.add(s.substring(position, s.length()));
return;
}
}
}

Related

Finding the nth term in a sequence

I have a sequence, and I am trying to make a program to find the nth term of the sequence.
The sequence is as follows:
1, 11, 21, 1211, 111221, 312211...
In this sequence, each term describes the previous term. For example, "1211" means that the previous term; the previous term is "21" where there is one occurrence of a 2 and then one occurrence of a 1 (=1211). To get the third term, "21," you look at the second term: 11. There are two occurrences of a 1 which gives us "21."
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
System.out.println( Main.num(n-1, "1"));
}
public static String num(int times, String x){
if(times == 0){
return x;
}else{
//System.out.println("meow");
String y = "" + x.charAt(0);
int counter = 0;
for(int i = 1; i < x.length(); i++){
if(x.charAt(i) == x.charAt(i-1)){
counter++;
}else{
y += "" + counter + x.charAt(i-1);
counter = 0;
}
}
return num(times--, y);
}
//return "";
}
}
My code uses recursion to find the nth term. But, it gives us errors :(
First, I start of the method "num" by passing it the number of terms-1 (since the first term is already given) and the first term (1).
In the method num, we start off by using a conditional to establish the base case (when you are done finding the nth term).
If the base case is false, then you find the next term in the sequence.
This is a very cool sequence! I like that it is English based and not mathematical, haha. (Though now I wonder ... is there a formula we could make for the nth term? I'm pretty sure it's impossible or uses some crazy-level math, but just something to think about ...)
In your solution, the recursive logic of your code is correct: after you find each term, you repeat the method with your knew number and find the next term using that element, and end when you have determined the first n elements. Your base case is also correct.
However, the algorithm you developed for determining the terms in the sequence is the issue.
To determine the next element in the sequence, we want to:
Logical Error:
Create a empty variable, y, for your next element. The variable, counter, should not start at 0, however. This is because every element will ALWAYS have an occurrence of at least 1, so we should initialize int counter = 1;
Iterate through the characters in x. (You did this step correctly) We begin at i = 1, because we compare each character to the previous one.
If the current character is equal to the previous character, we increment counter by 1.
Otherwise, we concatenate counter and the character being repeated, to y. Remember, to reinitialize counter to 1, not 0.
Technical Errors:
Once we reach the end of iterating x, we need to concatenate our final counter and character to y, since the else statement for the final characters will never run in our for loop.
This is done with the following code: y += "" + counter + x.charAt(x.length() - 1);
Finally, when you are doing your recursive call, you should do --times instead of times--. The difference between these two parameters is that with your original code, you are post-decrementing. This means the value of times is decreasing after the method call, when we want the decreased value to be sent into the method. To solve this, we need to pre-decrement, by doing --times.
import java.util.*;
class CoolSequence {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
System.out.println(num(n, "1"));
}
public static String num(int times, String x){
if(times == 0){
return x;
}
else{
String y = "";
int counter = 1;
for(int i = 1; i < x.length(); i++){
if(x.charAt(i) == x.charAt(i - 1)){
counter++;
}
else{
y += "" + counter + x.charAt(i - 1);
counter = 1;
}
}
y += "" + counter + x.charAt(x.length() - 1);
return num(--times, y);
}
}
}
Testing:
6
13112221
An alternative approach would be using an iterative method:
import java.util.*;
class CoolSequence2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<String> nums = new ArrayList<String>();
int n = scan.nextInt();
String val = "1";
for(int i = 0; i < n; i++){
String copy = val;
val = "";
while(!copy.equals("")){
char curr = copy.charAt(0);
int ind = 0;
int cons = 0;
while(ind < copy.length() && curr == copy.charAt(ind)){
cons += 1;
ind += 1;
}
val += String.valueOf(cons) + copy.charAt(cons - 1);
copy = copy.substring(cons);
}
nums.add(val);
}
System.out.println(nums.get(nums.size() - 1));
}
}
6
13112221
In this method, we use a for loop to iterate through n terms. To determine each element, we do a similar method to your logic:
We create an empty string, val, to hold the new element, and store our current element in copy. We also initialize a cons, similar to your counter.
While copy is not empty, we iterate through copy and increment cons until there is an element that is not equal to the next element.
When this occurs, we concatenate cons and the repeated element to val, like in your code. Then, we cut out the repeated elements from copy and continue the process.
We add the new value of val to nums, and keep iterating through the n elements.
I hope these two methods of approaching your problem helped! Please let me know if you have any further questions or clarifications :)
You can use Pattern with backreference.
The regular expression "(.)\\1*" matches any single character ("(.)") and zero or more sequences of the same character ("\\1*"). "\\1" is called a backreference, it refers to the string enclosed in parentheses 1st.
For example, it matches 111, 22 and 1 for 111221.
replaceAll() calls the lambda expression specified by the argument each time it matches. The lambda expression receives a MatchResult and returns a string. The matched string is replaced with the result.
The lambda expression in this case concatenates the length of the matched string (match.group().length()) and the first character (match.group(1)).
static final Pattern SEQUENCE_OF_SAME_CHARACTER = Pattern.compile("(.)\\1*");
static String num(int times, String x) {
for (int i = 0; i < times; ++i)
x = SEQUENCE_OF_SAME_CHARACTER.matcher(x).replaceAll(
match -> match.group().length() + match.group(1));
return x;
}
public static void main(String[] args) {
for (int i = 1; i <= 8; ++i)
System.out.print(num(i - 1, "1") + " ");
}
output:
1 11 21 1211 111221 312211 13112221 1113213211

Why am I doing i < a.length()-1 in my for loop

The question is: Given two Strings, return the number of the positions where they contain the same substring with a length of 2 .
The code I wrote works but my question is why do I have to add the minus 1 to line 3 which is this part: i<a.length()-1
(I know it has something to do with me using (i,i+2))
public int stringMatch(String a, String b) {
int count = 0;
if (a.length() < b.length()) {
for (int i=0; i<a.length()-1; i++) {
if (a.substring(i,i+2).equals(b.substring(i,i+2))) {
count++;
}
}
} else {
for (int i=0; i<b.length()-1; i++) {
if (b.substring(i,i+2).equals(a.substring(i,i+2))) {
count++;
}
}
}
return count;
}
Since the substring goes from the index i (included) to i + 2 (excluded), in order not to go out of the substring the loop needs to finish 1 step before.
String.length() function returns length of the string, but when you access the characters you access it using index. That is why you have to do "-1".
For Example:
String s = "Hello";
Length is 5.
Index is from 0 to 4.

Finding the original index after stripping a string

I have a function like the following:
int getIndex(String noisyString) {
String quietString = noisyString.replaceAll("[^a-z]", "");
int quietStringIndex = findIndexInQuietString(quietString);
return originalIndexInNoisyString; // ???
}
After stripping a string of all non alphabetical characters, I find an arbitrarily chosen index inside the stripped string. How can I convert this index back to one that can be used for the unstripped string?
It sounds like you are trying to get the index in the non-filtered string of the same character at the chosen index in the filtered string.
(ie. you have a String s1 = "abc123def" s1.replaceAll() = "abcdef". You want to get the original index of the character at index 4 in the filtered String.
The character at index 4 in the filtered String is e. Its index value in the unfiltered String is 7.)
The simplest brute force way to do this would be to use a counter to go through the string keeping track of what index you are up to, meanwhile having a separate counter variable to keep track of how many characters that have been passed that are valid for the filtered string.
public static int getOriginalIndex(String s, int index){
if (index > s.replaceAll("[^a-z]", "").length()) {
throw new IllegalArgumentException("index is invalid");
}
int counter;
int validCharCounter = 0;
for (counter = 0; counter < s.length() && validCharCounter < index; counter++) {
if (s.charAt(counter) >= 'a' && s.charAt(counter) <= 'z')
validCharCounter++;
}
return counter;
}

Matching subsequence of length 2 (at same index) in two strings

Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring. For instance a and b is respectively "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings.
What is wrong with my solution?
int count=0;
for(int i=0;i<a.length();i++)
{
for(int u=i; u<b.length(); u++)
{
String aSub=a.substring(i,i+1);
String bSub=b.substring(u,u+1);
if(aSub.equals(bSub))
count++;
}
}
return count;
}
In order to fix your solution, you really don't need the inner loop. Since the index should be same for the substrings in both string, only one loop is needed.
Also, you should iterate till 2nd last character of the smaller string, to avoid IndexOutOfBounds. And for substring, give i+2 as second argument instead.
Overall, you would have to change your code to something like this:
int count=0;
for(int i=0; i < small(a, b).length()-1; i++)
{
String aSub=a.substring(i,i+2);
String bSub=b.substring(i,i+2);
if(aSub.equals(bSub))
count++;
}
}
return count;
Why I asked about the length of string is, it might become expensive to create substrings of length 2 in loop. For length n of smaller string, you would be creating 2 * n substrings.
I would rather not create substring, and just match character by character, while keeping track of whether previous character matched or not. This will work perfectly fine in your case, as length of substring to match is 2. Code would be like:
String a = "iaxxai";
String b = "aaxxaaxx";
boolean lastCharacterMatch = false;
int count = 0;
for (int i = 0; i < Math.min(a.length(), b.length()); i++) {
if (a.charAt(i) == b.charAt(i)) {
if (lastCharacterMatch) {
count++;
} else {
lastCharacterMatch = true;
}
} else {
lastCharacterMatch = false;
}
}
System.out.println(count);
The heart of the problem lies with your usage of the substring method. The important thing to note is that the beginning index is inclusive, and the end index is exclusive.
As an example, dissecting your usage, String aSub=a.substring(i,i+1); in the first iteration of the loop i = 0 so this line is then String aSub=a.substring(0,1); From the javadocs, and my explanation above, this would result in a substring from the first character to the first character or String aSub="x"; Changing this to i+2 and u+2 will get you the desired behavior but beware of index out of bounds errors with the way your loops are currently written.
String a = "xxcaazz";
String b = "xxbaaz";
int count = 0;
for (int i = 0; i < (a.length() > b.length() ? b : a).length() - 1; i++) {
String aSub = a.substring(i, i + 2);
String bSub = b.substring(i, i + 2);
if (aSub.equals(bSub)) {
count++;
}
}
System.out.println(count);

Find all substrings of a string - StringIndexOutOfBoundsException

I created class Word. Word has a constructor that takes a string argument and one method getSubstrings which returns a String containing all substring of word, sorted by length.
For example, if the user provides the input "rum", the method returns a
string that will print like this:
r
u
m
ru
um
rum
I want to concatenate the substrings in a String, separating them with a newline ("\n"). Then return the string.
Code:
public class Word {
String word;
public Word(String word) {
this.word = word;
}
/**
* Gets all the substrings of this Word.
* #return all substrings of this Word separated by newline
*/
public String getSubstrings()
{
String str = "";
int i, j;
for (i = 0; i < word.length(); i++) {
for (j = 0; j < word.length(); j++) {
str = word.substring(i, i + j);
str += "\n";
}
}
return str;
}
But it throws exception:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1911)
I stuck at this point. Maybe, you have other suggestions according this method signature public String getSubstrings().
How to solve this issue?
Analysis of Exception:
From Java7 Docs of StringIndexOutOfBoundsException
public class StringIndexOutOfBoundsException extends IndexOutOfBoundsException
Thrown by String methods to indicate that an index is either negative or greater than the size of the string.
From Java 7 Docs of substring
public String substring(int beginIndex,int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
I guess this: length of the substring is endIndex-beginIndex comes into String index out of range: -1. I have tested with multiple cases holding my assumption true but appreciate any other proof.
For -1: "rum".substring(2,1); will give you String index out of range: -1
Parameters:
beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.
Cause of StringIndexOutOfBoundsException:
In the given code snippet, substring is trying to fetch string which has endIndex more than the total length of String (i+j will exceed the total length of string):
str = word.substring(i, i + j);
Consider the case when i=2 and j=2 for word "rum"
then str=word.substring(2, 4);
would not be possible
Solution similar to code snippet given in Question:
This should solve the problem:
public String getSubstrings()
{
String str="",substr = "";
for (int i = 0; i < word.length(); i++) {
for (int j = 0; i+j <= word.length(); j++) { //added i+j and equal to comparison
substr = word.substring(j, i + j); //changed word.substring(i, i + j) to word.substring(j, i + j)
if("".equals(substr))continue; //removing empty substrings
str += substr; //added concatenation + operation
str += "\n";
}
}
return str+word;
}
Test Case:
For word="rum", this will give output:
r
u
m
ru
um
rum
Your logic seems convoluted , the source of exception:
str = word.substring(i, i + j);
Consider your i and j both equals word.length()-1 , then the substring() will fail.
You can simply do :
public String getSubstrings(String word){
StringBuilder sub= new StringBuilder();
for( int i = 0 ; i < word.length() ; i++ )
{
for( int j = 1 ; j <= word.length() - i ; j++ )
{
sub .append(word.substring(i, i+j)).append("\n");
}
}
return sub.toString();
}
Note: Consider using StringBuilder instead of String if you will do lots of concatenation on String.
I realize I'm a little late to this party, and I'm a very new programmer, myself -- but I was running into the same error last night while trying to write a similar method.
For me, it helped to rename the counter variables of the nested for loops to names that described what they are keeping track of. For the outer loop, I used int subLength, and for the inner loop, I used int position (starting position). I'm sure there are other ways of doing this, but I was happy with my solution. Here is some pseudocode that I hope will help someone else who looks this question up:
for each possible substring length 1 up to and including the original word length:
generate substrings starting at the 0th position, and then starting at each
proceeding letter up to but not including (word.length() - (subLength - 1))

Categories

Resources