You are on page 1of 9

Strings and Languages

Strings
 Definition of Alphabet

• An alphabet is defined to be a
non-empty finite set whose
members are called the
symbols of the alphabet.

• The symbols can be letters,


numbers, special characters,
musical notes, emoticons, etc.

Examples:

Σ1 = {0, 1}
Σ2 = {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}

Σ3 = {♠, ♣, ♥, ♦}
F93

 Strings

• A string over an alphabet is a


finite sequence of 0 or more
symbols from that alphabet
juxtaposed or placed side by
side (not separated by commas).

Examples:

For the binary alphabet Σ1,


000, 101010, 11, and 11010
are strings that can be formed
over Σ1.
For the alphabet Σ2, abba, xyz,
sti, and abracadabra are
strings that can be formed
over Σ2.
• A string may contain no symbols
at all, and is called the empty
string, designated as ε.
• Let w be a string over an
alphabet Σ . The length of string
w is the number of symbols
contained in the string.

The length of a string w is


written as w.

For example:

If w = 010101 is a string over


the alphabet Σ = {0, 1}, then

w= 6

The length of the empty string is


0.
• A string w over alphabet Σ
whose length is k can be written
as

w = w1w2 … wk

where w1, w2, …, and wk are


elements of the alphabet Σ.
The reverse of a string w,
designated as wR, can be
defined as the string written in
the reverse order. Specifically,
if

w = w1w2 … wk

then its reverse is

wR = wk … w2w1

For example:

w = abcdef

wR = fedcba
Strings and Languages
• A string z is a substring of the
string w if z appears
consecutively within w.

For example:

If string z = def and string


w = abcdefgh, then it can
be said that z is a substring
of w.

Like in sets, a string may be a


substring to itself.

Similarly, the empty string ε is a


substring of any string.

Strings and Languages


• A prefix of a string is a substring
that is composed of any number
of leading symbols of that string.

For example:

If w = 11100101, then
some of the possible
prefixes of w are ε, 1, 11,
111, 1110, and 11100.

• A suffix of a string is a substring


that is composed of any number
of trailing symbols of that string.

For example:

If w = 11100101, then
some of the possible
prefixes of w are ε, 1, 01,
101, 0101, and 00101.
Strings and Languages
Page 6 of 9
• A fundamental operation that
can be performed on strings is
concatenation.

Given string x of length m and


string y of length n, the
concatenation of x and y, written
as xy, is the string obtained by
writing the string x followed by
the string y with no
intervening space between
them, as in

xy = x1…xmy1…yn.

For example:

If string x = sti and


string y = hq, then the
concatenation of x and y is

xy = stihq
Strings and Languages
Page 7 of 9
• Strings can be arranged in
lexicographic order.
The lexicographic ordering of
strings is the same as
alphabetical ordering except that
shorter strings precede longer
strings.
For example:
Given the following strings:
000, 01, 010, 011, 0, ε,
1101, 0110, and 1111.
Arranging them in
lexicographic order would
yield:
ε, 0, 01, 000, 010, 011,
0110, 1101, 1111
The lexicographic ordering of
strings depends of course on
how the individual ordering of
the elements of the alphabet is
defined.
Strings and Languages
Page 8 of 9
Languages
 Definition of Language

• A language is a set of strings


over a given alphabet.

For example:

Given the alphabet Σ = {0, 1}.


If the language L1 is defined as
the set of all 3-bit binary
numbers, then:

L1 = {000, 001, 010, 011,


100, 101, 110, 111}

Given the alphabet Σ = {0, 1}.


If the language L2 is defined as
the set of all palindromes,
then:

L2 = {ε, 0, 1, 00, 11, 010,


101, 110011, …}

Strings and Languages


Page 9 of 9

You might also like