I am trying to understand how String length() function work while reversing a string.
String length is 4 then why i need to give length()-1 in below working code.
No issue in below code, need help to understand length()
public class MStringReverse {
String getReverse(String input) {
System.out.println(input.length());
String reverse = "";
for(int i = input.length() - 1; i >= 0; i--) {
reverse = reverse + input.charAt(i);
}
return reverse;
}
public static void main(String[] args) {
MStringReverse mr = new MStringReverse();
String result = mr.getReverse("Achilis");
System.out.println(result);
}
}
As the index starts from 0, not from 1. So if you have a String of length 4 then 0,1,2,3 are the only possible indexes. If your index provided as the argument in charAt() is less than 0 or greater than or equals the length of the String then you will get StringIndexOutOfBoundsException exception. Here you can see how charAt method works :
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
That's because indexing starts at 0.
charAt(0) is character 1.
The answer is that you are iterating on indexes, which start at 0.
Imagine an array of length 4. It will store 4 items, the first one at index 0, second at index 1, third at 2 and the last one at index 3. The index of the last element is always length() - 1, that's why you put it as the upper border in loops in order to not raise an IndexOutOfBoundsException while iterating.
You could add some console output to view the accessed indexes per String for each iteration like this:
public class MStringReverse {
static String getReverse(String input) {
System.out.println("Input length is " + input.length());
String reverse = "";
for(int i = input.length() - 1; i >= 0; i--) {
System.out.println("accessing index " + i + " of \"input\"");
reverse = reverse + input.charAt(i);
System.out.println("last index of \"reverse\" is now " + (reverse.length() - 1));
}
return reverse;
}
public static void main(String[] args) {
String result = getReverse("Achilis");
System.out.println(result);
}
}
Because your string index starts at 0. Your length is 7. If you access input.charAt(7) you will get an index out of range exception.
A c h i l i s
0 1 2 3 4 5 6
The last index of the String is 1 less than the length because of a 0-based index.
i.e.
abcd has a length of 4 but to iterate from the last character, you need to start at index 3 (which is d), therefore length()-1 is where you start.
String length is always less than one because it starts index position from 0 .
if the String length is 4 then index position starts from 0 to 3
For this, you just understand the concept of the array.
Suppose we have an array of int of size 5.
So if size is 5 that means array index is from 0 to 4 last index always -1 from actual size.
Same apply on String length method in case of reversing the string.
Suppose you
String name = "Stack";
Its length is 5 but the last index is 4 because of the last index always -1 from actual length.
Related
Write a function subLength() that takes 2 parameters, a string and a single character. The function should search the string for the two occurrences of the character and return the length between them including the 2 characters. If there are less than 2 or more than 2 occurrences of the character the function should return 0.
// Write function below
const subLength = (str, char) => {
let charCount = 0;
let len = -1;
for (let i=0; i<str.length; i++) {
if (str[i] == char) {
charCount++;
if (charCount > 2) {
return 0;
}
if (len == -1) {
len = i;
} else {
len = i - len + 1
}
}
}
if (charCount < 2) {
return 0;
}
return len;
};
Can someone explain the len=-1 and how to find length between character part in this question please?
It is used as initial value. The initial value of 'len' needs to be outside the possible range of 'len' otherwise you cannot set the first position of the first occurrence of the char.
E.g. if 'len' is initialized with 0 it will be assumed that the first occurrence of the char is at position 0.
In your function the second line, you declare that len variable to -1. The intent is to use a number, that impossible to return (you defined 0 as no, or too many occurences, and every other number could be a valid length between those two occurences).
If you've found the first occurence of the specific character, you should remember it, where to start the counting. This is when it checks for the initial value of -1.
When the second character is found, this len variable is something other than -1, so the else branch is executed.
if(len == -1) {
len = i; // found the first occurence, save its position
} else {
len = i - len +1; // not the first occurence, calculate the length
}
variable
len has been initialized with -1 because this value is not going to occur as index or length while iterating over the String
str. Later,
len is initialized with the index at which that char occurs first time in String
str by checking if
len is -1 otherwise it would be second occurrence of same
char(else condition).
I am given a string from which i have to find out sub-strings that satisfy the following conditions
all characters in the sub-string are same. eg: aa,bbb,cccc.
all the character except the middle character have to be the same.
eg: aba, bbabb, etc.
I've made an algo something like this
I beak the string using two loops 1st loop holds the first char and the second loop traverses through the string.
Then i send the sub-string to the vet() to see if the substring contains less than or equals two character.
If the sub-string contains two character then i check if its a palindrome
public static int reverse(String s)
{
String wrd="";
for(int i = s.length()-1 ;i>=0;i--)
wrd = wrd + s.charAt(i);
if(s.equals(wrd))
return 1;
else
return 0;
}
public static boolean vet(String s)
{
HashSet<Character> hs = new HashSet<>();
for(char c : s.toCharArray())
{
hs.add(c);
}
if(hs.size() <= 2)
return true;
else
return false;
}
static long substrCount(int n, String s) {
List<String> al = new ArrayList<>();
for(int i=0;i<s.length();i++)
{
for(int j=i;j<s.length();j++)
{
if(vet(s.substring(i,j+1)))
{
if(reverse(s.substring(i,j+1)) == 1)
al.add(s.substring(i,j+1));
}
}
}
return al.size();
}
This code works fine for small strings, however if the string is big say ten thousand character, this code will throw Time limit exception.
I suspect the loop that breaks the string and create the sub-string in the substrCount() is causing the time complexity as it has nested loops.
Please review this code and provide a better way to break the string or if the complexity is increasing due to some other section then let me know.
link : https://www.hackerrank.com/challenges/special-palindrome-again/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=strings
You can collect counts from left side and right side of the string in 2 separate arrays.
Now, we collect counts in the fashion of if previous char equals current char, increase count by 1, else set it to 1.
Example:
a a b a a c a a
1 2 1 1 2 1 1 2 // left to right
2 1 1 2 1 1 2 1 // right to left
For strings that have all characters equal, we just collect all of them while iterating itself.
For strings with all equal except the middle character, you can use above the above table and you can collect string as below:
Pseudocode:
if(str.charAt(i-1) == str.charAt(i+1)){ // you will add checks for boundaries
int min_count = Math.min(left[i-1],right[i+1]);
for(int j=min_count;j>=1;--j){
set.add(str.substring(i-j,i+j+1));
}
}
Update:
Below is my accepted solution.
static long substrCount(int n, String s) {
long cnt = 0;
int[] left = new int[n];
int[] right = new int[n];
int len = s.length();
for(int i=0;i<len;++i){
left[i] = 1;
if(i > 0 && s.charAt(i) == s.charAt(i-1)) left[i] += left[i-1];
}
for(int i=len-1;i>=0;--i){
right[i] = 1;
if(i < len-1 && s.charAt(i) == s.charAt(i+1)) right[i] += right[i+1];
}
for(int i=len-1;i>=0;--i){
if(i == 0 || i == len-1) cnt += right[i];
else{
if(s.charAt(i-1) == s.charAt(i+1) && s.charAt(i-1) != s.charAt(i)){
cnt += Math.min(left[i-1],right[i+1]) + 1;
}else if(s.charAt(i) == s.charAt(i+1)) cnt += right[i];
else cnt++;
}
}
return cnt;
}
Algorithm:
The algorithm is the same as explained above with a few additional stuff.
If the character is at the boundary, say 0 or at len-1, we just look at right[i] to count the strings, because we don't have a left here.
If a character is inside this boundary, we do checks as follows:
If previous character equals next character, we check if previous character does not equal current character. We do this because, we want to avoid future addition of strings at the current iteration itself(say for strings like aaaaa where we are at the middle a).
Second condition says s.charAt(i) == s.charAt(i+1), meaning, we again have strings like aaa and we are at the first a. So we just add right[i] to indicate addition of strings like a,aa,aaa).
Third does cnt++ meaning addition of individual character.
You can make a few optimizations like completely avoiding right array etc, but I leave that to you as an exercise.
Time complexity: O(n), Space complexity: O(n)
Your current solution runtime is O(n^4). You can reduce it to O(n^2logn) by removing the number of character count in substrings and optimising the palindrome check portion.
To do so, you have to pre-calculate an array say "counter" where every position of the "counter" array indicates number of different characters from starting index to that position.
After constructing the array, you can check if a substring has more than two characters in O(1) by subtracting the end position and starting position value of counter array. If the value is 1 then there have only one character in the substring. If the value is 2, then you can binary search in the counter array between the substrings starting and end positions to find the position of single character. After finding out the position of the single character its straight forward to check if the substring is palindrome or not.
UPDATE!
Let me explain with an example:
Suppose the string is "aaabaaa".
So, the counter array would be = [1, 1, 1, 2, 2, 2, 2];
Now, lets assume for a specific time, the outer for loops value i = 1 and the inner for loops value j = 5; so the substring is "aabaa".
Now to find the number of character in the substring by following code:
noOfDifferentCharacter = counter[j] - counter[i-1] + 1
If the noOfDifferentCharacter is 1 then no need to check for palindrome. If the noOfDifferentCharacter is 2 like in our case we need to check if the substring is palindrome. To check if the substring is palindrome have to perform a binary search in the counter array from index i to j to check for the position where the value is greater than its previous index. In our case the position is 3, then you just need to check if the position is the middle position of the substring. Note that the counter array is sorted.
Hope this helps. Let me know if you don't understand any step. Happy coding!
My code below is giving the following error and I can't figure out why. I am trying to reorder the entered word ("Polish" for example) in the order of:
(First letter, last letter, second letter, second last letter, third letter... so on) so the output should be "Phosli".
Updated code
public static String encodeTheWord(String word1)
{
int b = 0;
int e = word1.length()-1;
String word2 = "";
for (int i=0; i<e; i++)
{
word2 = word2 + word1.charAt(b) + word1.charAt(e);
b+=1;
e-=1;
}
System.out.println(word2);
return (word2);
}
For a word with an even amount of characters (Polish), the order of the characters becomes 051423, so the maximum value of b is 2 and the minimum value is e is from 5 to 3. Thus, your loop should decrement e and increment b twice (so you run the loop for word1.length() / 2 times). Also,
int e = word1.length();
Would need to be:
int e = word1.length() - 1;
For words of an uneven length (word1.length() % 2 > 0) you need an extra check or you will repeat the middle character.
your for loop is wrong, you can get a char at index 0, until the word1.length()-1...
must be
for (int i=0; i<word1.length()-1; i++)
the same applies for this...
word1.charAt(e);
because you defined e as word1.length()
I'm not allowed to use methods from any class except String and IO Class
So my code snippet is:
String line = reader.readLine();
while (line != null) {
String[] elements = line.split(",");
// Array could be too big if there are multiple occurances of
// the same number
// Array length + 1 because I can't use the 0 and with a input line of
// 1,2,3 for example would be the length 3 but I would have the
// numbers 0,1,2 in the Array as my index.
String[][] placeholderMatrix = new String[elements.length+1][elements.length+1];
for(int i = 0; i < elements.length-1; i++){
placeholderMatrix[(int)elements[i]][(int)elements[i+1]] = 1;
}
line = reader.readLine();
}
In the File I'm getting are only numbers like that: 1,2,3,4,5,8,7,4
So in my splitted String Array are only Numbers but now if I want to use them as my index for my Matrix(placeholderMatrix)
My problem is in my for loop where I want to use them as my Index I can't use them because it is a String Array. Normally I would use Integer.parseInt but I'm not allowed to :/
Any ideas on how I can implement them as my Index? and any Idea how I can get the perfect length of my Matrix? Because If I get the following numbers: 1,2,2,2,3 My Matrix should only have the numbers:
0 1 2 3
1
2
3
But if I'm using elements.length+1 for the length of my Matrix I would get the numbers 0 1 2 3 4 5
Hope you could understand my problem. Sorry for my bad english and Thanks in advance.
Edit: SO i got another problem with that. If I implement the method(parseInt) of Dici and am using it in the line "placeholderMatrix[parse(elements[i])][parse(elements[i+1])] = 1;" I'm getting the error ArrayOutOfBounce because my defined Array is just the length of my splitted String Array elements. But if I define it with Integer.MAX_VALUE as my length I get a memory error because it is too big. Any ideas?
Edit2: My Task:
I have to take a row of Numbers seperated by ",". (I will split it with the String split method to get only the numbers) Now I have to create a Matrix(2 dimensional Array) and look for the number at the index i of my new String Array and the number at the index i + 1 and have to take the first Number as my column and th second as my row (or vice versa) and implement at that point a 1. Now are my Numbers I will get from 1 to Integer.MAX_VALUE so I would have to create such a big Matrix but this isn't possible because I get the MemoryError.
Error: java.lang.OutOfMemoryError: Requested array size exceeds VM limit
at Test.main(Test.java:29)
To understand what I have to do: http://de.wikipedia.org/wiki/Adjazenzmatrix the image at the right but for numbers from to Integer.MAX_VALUE so my 2D Array has to be defined with the length of Integer.MAX_VALUE?
Edit:
So Dici asked for an example:
My Sequence could be: 1,2,5,4
So my Matrix should be:
Hope this is what you wanted Dici
But the numbers I can get from the sequence are 1 to Integer.MAX_VALUE
For converting strings to integers, you can simply implement your own integer parser, it is not complicated. You can start with this and improve it if needed.
public int parseInt(String s) {
int n = 0;
int pow = 1;
for (int i=s.length() - 1 ; i>=0 ; i--) {
String si = String.valueOf(s.charAt(i);
if (si.matches("[0-9]")) {
n += pow*(s.charAt(i) - '0');
pow *= 10;
} else if (si.matches("+|-") && i == 0)
n *= s.charAt(i) == '+' ? 1 : -1;
else
throw new NumberFormatException();
}
return n;
}
Then, I'll handle the second part of your problem. If Integer.MAX_VALuE is one of your input values, you cannot possibly allocate an Integer.MAX_VALUE x Integer.MAX_VALUE matrix. What you need to do is assign contiguous ids to your input values and record the ids in a map so that you can access easily the index of the matrix corresponding to one node value. Here is an example to get you to understand :
public void someMethod() {
int id = 0;
Map<Integer,Integer> idMap = new HashMap<>();
String[] split = reader.readLine().split(",");
int [] nodes = new int[split.length];
for (int i=0 ; i<nodes.length ; i++) {
nodes[i] = parseInt(split[i]);
if (!idMap.containsKey(nodes[i]))
idMap.put(nodes[i],id++);
}
// the map is now constructed, it should probably be stored in an attribute
int[][] placeholderMatrix = new int[nodes.length][nodes.length];
for(int i = 0; i < nodes.length; i++){
if (i > 0) placeholderMatrix[idMap.get(nodes[i])][idMap.get(nodes[i-1])] = 1;
if (i < nodes.length-1) placeholderMatrix[idMap.get(nodes[i])][idMap.get(nodes[i+1])] = 1;
}
}
There are other ways to do it, let me know if this solution is ok
You could do something like:
String keyword = "1,2,3,4,5,8,7,4";//input line from file
String replacedKeyword = keyword.replaceAll("[^\\d]", "");//except numbers replace all. Assuming one digit numbers only.
String[][] placeholderMatrix = new String[replacedKeyword.length()+1][replacedKeyword.length()+1];
char keys[] = replacedKeyword.toCharArray();
for (int i = 0; i<keys.length - 1; i++) {
placeholderMatrix[keys[i] - '0'][keys[i + 1] -'0'] = "1";
}
I couldn't really understand what you want exactly. but, if that going to help a simple method to convert String number to int:
int toInt(String number) {
int num = 0;
for (int i=0; i<number.length(); i++) {
num = num*10 + (number.charAt(i)-'0');
}
return num;
}
I have an array of 28 characters. I'm trying to use a for loop to print the string and its length so that the next line removes the last character from the previous line. For example:
7 Example
6 Exampl
5 Examp
4 Exam
3 Exa
2 Ex
1 E
I have this so far, but it's not printing. What am I doing wrong?
for(int index=28; index <= nameArray.length; index--)
{
System.out.print(nameArray[index]);
}
It is as simple as that ..
class A {
public static void main(String [] args) {
String array = "Example";
for(int i=7;i>0;i--) {
System.out.print(i+" ");
System.out.println(array.substring(0, i));
}
}
}
// Output :
7 Example
6 Exampl
5 Examp
4 Exam
3 Exa
2 Ex
1 E
If the length is 28 characters, you cannot start the index at 28. Arrays in Java are zero-based, so that means that the index can range from 0 to 27 (length - 1).
Firstly, you probably want your middle condition to be index > 0, not index <= nameArray.length (which should always be true, even as you enter the negative numbers).
Secondly, is it an array of characters you're accessing? If so, then as I'm sure you know, nameArray[index] refers to a character, not a string.
(Also, as bobbymcr points out, the top index in an array of 28 elements is 27, not 28.)
To print substrings, you should first initialize a String using your character array, then print increasingly smaller substrings using substring.
For example:
private static void printSubstrings(String input) {
for (int length = input.length(); length > 0; length--) {
System.out.printf("%d %s\n", length, input.substring(0, length));
}
}
Then, if nameArray is a char[], you could use the String(char[]) constructor to pass to this method:
printSubstrings(new String(nameArray));
Example input:
char[] nameArray = new char[] { 'E', 'x', 'a', 'm', 'p', 'l', 'e' };
printSubstrings(new String(nameArray));
Output:
7 Example
6 Exampl
5 Examp
4 Exam
3 Exa
2 Ex
1 E
Always remeber that the array indexes start from 0
[E][x][a][m][p][l][e]
0 1 2 3 4 5 6
I don't understand why you break your head with char arrays, when you have String.
Here i propose you a solution where:
1-transform the char[] to String
2-Iterate the String object and use the some methods from the String class to get the desired result:
public class Example
{
public static void main(String[] args) {
//Here an array with 28 characters
char[] array = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'
,'q','r','s','t','u','v','w','x','y','z','!','#'};
//For easier manipulation i will just transform that array to String
//Why not, String was invented so we dont have to use char[].
String arrayAsString = new String(array);
//Use the methods that String class provides, to make the solution easier
for(int i=0;i < array.length;i++) {
System.out.println((i+1) + " " + arrayAsString.substring(0,(arrayAsString.length()-i)));
}
}
}
for(int index=28; index <= nameArray.length; index--)
First off, you're trying to start at index 28 - that doesn't exist. Arrays are 0 based which means they go from 0 to length - 1
Second, you're saying that the index has to be less than or equal to the length. Since you're going from the end to the start, that would always be true.
for (int index = nameArray.length - 1; index >= 0; index--)
That's what you're looking for.