char A; // creates a character array of size 1000. char *A = (char *) malloc(1000 * sizeof(char)); // creates a character array of size 1000
Strings have to end with a '\0' character. All C input functions like scanf do it automatically. Direct string assignments like `char A = "Hello";` does it automatically as well.
strlen(A); // O(n) operation where n is length of string
C style char arrays work in C++ as well. However, C++ provides string which is much more powerful than C arrays. We will be using C++ strings in most of the problems on this judge.
string A; // declares an empty string string A = "Hello"; // declares string initialized to "Hello".
A[i] // O(1)
A.length() // O(1)
A += "Hello"; // Appends Hello to the string. O(n) operation A.push_back('H'); // Append character H to the string. O(1) operation.
Java has strings as well which are very similar to C++ string. However java strings are immutable. As such, if you plan to keep appending to the string, then better use StringBuilder.
String A = new String(); // declares an empty string. O(1) String A = new String("Hello"); // declares string initialized to "Hello" OR for mutable string : StringBuilder A = new StringBuilder(); // empty string StringBuilder A = new StringBuilder("Hello"); // stringbuilder initialized to "Hello".
A.charAt(i) // O(1). Works for both String and StringBuilder
A.length() // O(1) operation
String : A.concat("Hello"); // O(n) operation. Creates another copy of the string as string is immutable. StringBuilder : A.append("Hello"); // O(l) operation where l is the length of the string being appended. Much more efficient.
Python has string which is very similar to java string. Its immutable.
A = "" # empty string A = "Hello" # string initialized to "Hello"
A[i] # O(1)
A = A + "Hello" # O(n) operation and a new copy is created
An alternative way if you are going to do a lot of appends :
l =  l.append('string1') l.append('string2') l.append('string3') A = ''.join(l)
len(A) // O(1)
|Longest Palindromic Substring||500||