WEBVTT

00:00:00.790 --> 00:00:03.190
The following content is
provided under a Creative

00:00:03.190 --> 00:00:04.730
Commons license.

00:00:04.730 --> 00:00:07.030
Your support will help
MIT OpenCourseWare

00:00:07.030 --> 00:00:11.390
continue to offer high quality
educational resources for free.

00:00:11.390 --> 00:00:13.990
To make a donation or
view additional materials

00:00:13.990 --> 00:00:17.860
from hundreds of MIT courses,
visit MIT OpenCourseWare

00:00:17.860 --> 00:00:18.810
at ocw.mit.edu.

00:00:23.260 --> 00:00:26.860
PROFESSOR: We have
two strings, s1, s2.

00:00:29.800 --> 00:00:32.320
First, we're going to
check if the length of s1

00:00:32.320 --> 00:00:34.630
is equal length 2.

00:00:34.630 --> 00:00:37.120
Anyone, really quickly
can you tell me what

00:00:37.120 --> 00:00:40.666
is the length of s1 and s2?

00:00:40.666 --> 00:00:42.040
How many characters
are in there?

00:00:46.610 --> 00:00:48.770
10 right, because
we're including spaces.

00:00:48.770 --> 00:00:52.417
So 1, 2, 3, 4, 5,
6, 7, 8, 9, 10,

00:00:52.417 --> 00:00:53.750
so they are equal, first of all.

00:00:53.750 --> 00:00:56.970
So we're going to go
inside this if statement.

00:00:56.970 --> 00:01:00.810
Then we're going to go
for every character in s1.

00:01:00.810 --> 00:01:03.650
And then we're going
to have a nested loop.

00:01:03.650 --> 00:01:05.390
For every character
in s2, we're going

00:01:05.390 --> 00:01:07.931
to check if the two characters
are the same, and if they are,

00:01:07.931 --> 00:01:10.640
we're going to print this
common letter statement

00:01:10.640 --> 00:01:12.880
and then we're going to break.

00:01:12.880 --> 00:01:13.540
OK.

00:01:13.540 --> 00:01:16.800
Great we have some
good competition here.

00:01:16.800 --> 00:01:17.620
So let's see.

00:01:21.130 --> 00:01:23.350
The outer for loop
is first going

00:01:23.350 --> 00:01:27.550
to go through the
letter M. So this

00:01:27.550 --> 00:01:30.090
is-- so first it's going
to look at M, right.

00:01:33.170 --> 00:01:38.512
For char in s1, the first
character in s1 is an M.

00:01:38.512 --> 00:01:40.470
Then we're entering inside
this other for loop.

00:01:40.470 --> 00:01:42.660
For char 2 in s2.

00:01:42.660 --> 00:01:44.700
So I'm going to
say for this M, I'm

00:01:44.700 --> 00:01:47.690
going to look at I, which is
the first character in s2.

00:01:50.760 --> 00:01:53.910
If the character 1 is equal
to character 2, they're not,

00:01:53.910 --> 00:01:55.990
don't do anything.

00:01:55.990 --> 00:01:59.170
Keep going to the
next character in s2.

00:01:59.170 --> 00:02:01.210
So that-- you don't
do anything there.

00:02:01.210 --> 00:02:03.190
The next character
in s2 is a space,

00:02:03.190 --> 00:02:07.060
M does not equal a space
so you don't do anything.

00:02:07.060 --> 00:02:10.410
R does not-- the next character
is an R, don't do anything.

00:02:10.410 --> 00:02:12.830
The next character is
a U, don't do anything.

00:02:12.830 --> 00:02:14.182
Then an L, don't do anything.

00:02:14.182 --> 00:02:15.390
Then an E, don't do anything.

00:02:15.390 --> 00:02:16.860
Space.

00:02:16.860 --> 00:02:19.690
And then suddenly
we get to an M.

00:02:19.690 --> 00:02:23.650
So we're still in the outer for
loop, we're still looking at M,

00:02:23.650 --> 00:02:26.730
and we're comparing M with every
other letter inside the s2.

00:02:29.240 --> 00:02:32.480
And suddenly we reach here where
we're comparing M with an M

00:02:32.480 --> 00:02:34.710
and when we say
yes, they are equal.

00:02:34.710 --> 00:02:38.830
So we're going to
print out something.

00:02:38.830 --> 00:02:41.914
Make that into a better check.

00:02:41.914 --> 00:02:43.330
We're going to
print common letter

00:02:43.330 --> 00:02:45.680
and then we're going to break.

00:02:45.680 --> 00:02:49.370
This break says do
not keep looking

00:02:49.370 --> 00:02:52.550
at the remaining letters in s2.

00:02:52.550 --> 00:02:54.890
So once we've found
one that fits--

00:02:54.890 --> 00:02:58.100
once we found one the
matches, stop right here,

00:02:58.100 --> 00:03:07.000
break, and print out the thing,
and then go on to looking at I,

00:03:07.000 --> 00:03:09.160
which is the next letter in s1.

00:03:09.160 --> 00:03:11.850
And you do the same
thing, compare I with I,

00:03:11.850 --> 00:03:14.710
we have a match.

00:03:14.710 --> 00:03:16.600
Perfect, right off the bat.

00:03:16.600 --> 00:03:19.750
So you print out
another common letter.

00:03:19.750 --> 00:03:23.020
And then you break, which
means don't keep looking

00:03:23.020 --> 00:03:24.850
at the remaining letters.

00:03:24.850 --> 00:03:30.100
And then you look at-- since
you broke out of the inner loop,

00:03:30.100 --> 00:03:31.210
you go to the outer loop.

00:03:31.210 --> 00:03:34.640
The next letter is a
T and so on and so on.

00:03:34.640 --> 00:03:38.230
So essentially you're comparing
every letter out here in s1

00:03:38.230 --> 00:03:40.240
with every single
letter in s2 until you

00:03:40.240 --> 00:03:41.500
find one that matches.

00:03:41.500 --> 00:03:43.480
As soon as you do, you exit.

00:03:43.480 --> 00:03:45.250
So if you actually
run this program

00:03:45.250 --> 00:03:49.943
it's going to print out
seven times, I think.

00:03:49.943 --> 00:03:50.905
We can check it.

00:03:56.690 --> 00:03:59.690
1, 2, 3, 4, 5, 6, 7 times.

00:04:03.090 --> 00:04:03.705
Nice.

00:04:03.705 --> 00:04:05.430
The majority of the
people are right.

00:04:05.430 --> 00:04:07.650
If you got this wrong,
please go back and try

00:04:07.650 --> 00:04:09.000
to trace through your program.

00:04:09.000 --> 00:04:10.830
That means step by
step try to figure out

00:04:10.830 --> 00:04:12.810
what values of
variables are just

00:04:12.810 --> 00:04:14.820
to make sure that you're
on the right track

00:04:14.820 --> 00:04:16.950
and you understand
exactly what's happening.

00:04:16.950 --> 00:04:18.500
OK.