Strings: Making Anagrams
Strings: An Overview
Overview
Fundamentally, a string is a sequence of characters. Different languages implement them in different ways, but the basic string types in object-oriented languages tend to be immutable. This means you cannot directly modify them at all and must create a new object to hold any variation on the string.
When you're modifying a string a lot, it can get pretty costly in time and space to repeatedly recreate minor variants of the same string over and over. So what do you do when you have a real use-case for a mutable string? Some languages have additional classes that allow you to manipulate strings. For example, Java's String class is immutable, but you can use StringBuffer or StringBuilder for scenarios where you truly need a mutable sequence of characters.
Concatenation
When we concatenate two strings, we join them together so that the end of one string is followed by the beginning of the other or, more simply put, we append one string to another. Most languages use the +
operator for concatenating strings.
Let's say we have two strings, and . If we were to concatenate them and store the result in some third string, , we would get .
public class Example {
public static void main(String[] args) {
String a = "Hello";
String b = "World";
String c = a + b;
System.out.println(c);
}
}
While using and to create is very simple in this example, concatenation of immutable strings can get very costly in instances where you're making many minor modifications to the same string. If you need to perform repeated concatenations, you may want to consider using a mutable string class (or implementing your own).
Table Of Contents