I have a String/character
sequence that is being repeated infinitesimally... Naturally ,characters will go out of range of Integer and start falling into range of Long, since methods used for accessing characters for both String as well as StringBuilder
class all require an int "index"
how do I access these characters at say ,Long long>Intger.MAX_VALUE
. is there a way to override these methods such as charAt(int index)
so that they start "accepting " long arguments ,if not so , how can I access the characters at this index, considered conversion to character array using String.toCharArray()
method but then again, array length can only be upto Integer.MAX_VALUE
. Is there a method/constructor
type that I'm not aware of which accepts long arguments?
Asked
Active
Viewed 171 times
0

Young Emil
- 2,220
- 2
- 26
- 37

Hardik Vats
- 57
- 8
-
2the maximum length of a String is Integer.MAX_VALUE, afaik. There won't be any characters beyond that range – Stultuske Sep 20 '16 at 13:54
-
1Possible duplicate of [String's Maximum length in Java - calling length() method](http://stackoverflow.com/questions/816142/strings-maximum-length-in-java-calling-length-method) – Smutje Sep 20 '16 at 13:57
-
i cross checked .. you are correct ,Shouldve checked myself before asking,need to make modifications accordingly! – Hardik Vats Sep 20 '16 at 14:00
-
1A string consists of a character array and some additional information. Your string is completely theoretical. You have not created it, and you wouldn't be able to. Perhaps you should refer us to the actual problem that you think will require such a string. – RealSkeptic Sep 20 '16 at 14:00
-
@RealSkeptic Ok , So the problem is like is This Lilah has a string,s , of lowercase English letters that she repeated infinitely many times. Given an integer,n , find and print the number of letter a's in the first letters of Lilah's infinite string. Sample Input a 1000000000000 – Hardik Vats Sep 20 '16 at 14:04
-
1So you need to figure out what the repeated String `s` is, figure out how many "a"s there are in it, and how many times her String would fit into 1000000000000 characters. **This doesn't mean you have to actually construct a 1000000000000-char String!** It's a glorified math problem, not a String-management problem. – dcsohl Sep 20 '16 at 14:06
-
Is this the Philoshophical Programming forum? – gsl Sep 20 '16 at 14:08
-
yes , i understood how i need to handle this case and similar to it ,in a different manner an its just ludicrous to construct such a string! – Hardik Vats Sep 20 '16 at 14:10
-
But my point is, you don't have to construct the string, and probably shouldn't even consider it. Look, if the repeated String were "CaliforniaDreamin", that's 17 characters, so in the first 1000000000000 characters it repeats 58823529411 times, and then there are the first 13 characters again ("CalforniaDre"). So the number of "a"s here is 3 for each full repeat, and 2 for the partial at the end. 3*58823529411 + 2 = 176470588235. – dcsohl Sep 20 '16 at 14:14
-
Got it, Fresh perspective1 I just have to perform calculations based on the numbers provided! so its just like a math problem! – Hardik Vats Sep 20 '16 at 14:18
1 Answers
1
You should definitely not construct a string and do measurements on it.
This is a test on how well you are able to abstract things. I will give you some code you may study. You should not copy+paste it for several reasons - including the possibility that I did some mistake.
The idea is, to simply compute the information, which is possible because we have a simple repetition pattern.
class RepeatedString {
private String s;
public RepeatedString(String s) {this.s = s;}
public char charAt(long i) {
return s.charAt((int)(i % s.length()));
}
public long count(char c, long i) {
long n = 0;
// how many complete repetitions?
{
long r = i / s.length();
if (r > 0) {
// count c in s
for (int j = 0 ; j < s.length() ; j++) n += s.charAt(j) == c ? 1 : 0;
n *= r;
}
}
// how many c in last repitition
{
long l = i % s.length();
for (int j = 0 ; j < l ; j++) n += s.charAt(j) == c ? 1 : 0;
}
return n;
}
}
class Kata {
public static void main(String[] args) {
RepeatedString s = new RepeatedString("bla");
System.out.println(s.charAt(1)); // expected 'l'
System.out.println(s.charAt(6)); // expected 'b'
System.out.println(s.count('a', 19)); // expected 6
System.out.println(s.count('a', 21)); // expected 7
}
}

UniversE
- 2,419
- 17
- 24
-
Yes, the Comments section above gave me a fresh perspective towards this problem, and your code cemented that concept for me.. Of course, i will not be copy pasting this code ,since i might want to apply the same logic with a slightly different approach, but it was really helpful to get me familiar with the direction in which i should be thinking about this problem and any future problems i encounter with a similar pattern to it! – Hardik Vats Sep 20 '16 at 14:32