1 00:00:00,790 --> 00:00:03,190 The following content is provided under a Creative 2 00:00:03,190 --> 00:00:04,730 Commons license. 3 00:00:04,730 --> 00:00:07,030 Your support will help MIT OpenCourseWare 4 00:00:07,030 --> 00:00:11,390 continue to offer high quality educational resources for free. 5 00:00:11,390 --> 00:00:13,990 To make a donation or view additional materials 6 00:00:13,990 --> 00:00:17,860 from hundreds of MIT courses, visit MIT OpenCourseWare 7 00:00:17,860 --> 00:00:18,810 at ocw.mit.edu. 8 00:00:23,260 --> 00:00:26,860 PROFESSOR: We have two strings, s1, s2. 9 00:00:29,800 --> 00:00:32,320 First, we're going to check if the length of s1 10 00:00:32,320 --> 00:00:34,630 is equal length 2. 11 00:00:34,630 --> 00:00:37,120 Anyone, really quickly can you tell me what 12 00:00:37,120 --> 00:00:40,666 is the length of s1 and s2? 13 00:00:40,666 --> 00:00:42,040 How many characters are in there? 14 00:00:46,610 --> 00:00:48,770 10 right, because we're including spaces. 15 00:00:48,770 --> 00:00:52,417 So 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16 00:00:52,417 --> 00:00:53,750 so they are equal, first of all. 17 00:00:53,750 --> 00:00:56,970 So we're going to go inside this if statement. 18 00:00:56,970 --> 00:01:00,810 Then we're going to go for every character in s1. 19 00:01:00,810 --> 00:01:03,650 And then we're going to have a nested loop. 20 00:01:03,650 --> 00:01:05,390 For every character in s2, we're going 21 00:01:05,390 --> 00:01:07,931 to check if the two characters are the same, and if they are, 22 00:01:07,931 --> 00:01:10,640 we're going to print this common letter statement 23 00:01:10,640 --> 00:01:12,880 and then we're going to break. 24 00:01:12,880 --> 00:01:13,540 OK. 25 00:01:13,540 --> 00:01:16,800 Great we have some good competition here. 26 00:01:16,800 --> 00:01:17,620 So let's see. 27 00:01:21,130 --> 00:01:23,350 The outer for loop is first going 28 00:01:23,350 --> 00:01:27,550 to go through the letter M. So this 29 00:01:27,550 --> 00:01:30,090 is-- so first it's going to look at M, right. 30 00:01:33,170 --> 00:01:38,512 For char in s1, the first character in s1 is an M. 31 00:01:38,512 --> 00:01:40,470 Then we're entering inside this other for loop. 32 00:01:40,470 --> 00:01:42,660 For char 2 in s2. 33 00:01:42,660 --> 00:01:44,700 So I'm going to say for this M, I'm 34 00:01:44,700 --> 00:01:47,690 going to look at I, which is the first character in s2. 35 00:01:50,760 --> 00:01:53,910 If the character 1 is equal to character 2, they're not, 36 00:01:53,910 --> 00:01:55,990 don't do anything. 37 00:01:55,990 --> 00:01:59,170 Keep going to the next character in s2. 38 00:01:59,170 --> 00:02:01,210 So that-- you don't do anything there. 39 00:02:01,210 --> 00:02:03,190 The next character in s2 is a space, 40 00:02:03,190 --> 00:02:07,060 M does not equal a space so you don't do anything. 41 00:02:07,060 --> 00:02:10,410 R does not-- the next character is an R, don't do anything. 42 00:02:10,410 --> 00:02:12,830 The next character is a U, don't do anything. 43 00:02:12,830 --> 00:02:14,182 Then an L, don't do anything. 44 00:02:14,182 --> 00:02:15,390 Then an E, don't do anything. 45 00:02:15,390 --> 00:02:16,860 Space. 46 00:02:16,860 --> 00:02:19,690 And then suddenly we get to an M. 47 00:02:19,690 --> 00:02:23,650 So we're still in the outer for loop, we're still looking at M, 48 00:02:23,650 --> 00:02:26,730 and we're comparing M with every other letter inside the s2. 49 00:02:29,240 --> 00:02:32,480 And suddenly we reach here where we're comparing M with an M 50 00:02:32,480 --> 00:02:34,710 and when we say yes, they are equal. 51 00:02:34,710 --> 00:02:38,830 So we're going to print out something. 52 00:02:38,830 --> 00:02:41,914 Make that into a better check. 53 00:02:41,914 --> 00:02:43,330 We're going to print common letter 54 00:02:43,330 --> 00:02:45,680 and then we're going to break. 55 00:02:45,680 --> 00:02:49,370 This break says do not keep looking 56 00:02:49,370 --> 00:02:52,550 at the remaining letters in s2. 57 00:02:52,550 --> 00:02:54,890 So once we've found one that fits-- 58 00:02:54,890 --> 00:02:58,100 once we found one the matches, stop right here, 59 00:02:58,100 --> 00:03:07,000 break, and print out the thing, and then go on to looking at I, 60 00:03:07,000 --> 00:03:09,160 which is the next letter in s1. 61 00:03:09,160 --> 00:03:11,850 And you do the same thing, compare I with I, 62 00:03:11,850 --> 00:03:14,710 we have a match. 63 00:03:14,710 --> 00:03:16,600 Perfect, right off the bat. 64 00:03:16,600 --> 00:03:19,750 So you print out another common letter. 65 00:03:19,750 --> 00:03:23,020 And then you break, which means don't keep looking 66 00:03:23,020 --> 00:03:24,850 at the remaining letters. 67 00:03:24,850 --> 00:03:30,100 And then you look at-- since you broke out of the inner loop, 68 00:03:30,100 --> 00:03:31,210 you go to the outer loop. 69 00:03:31,210 --> 00:03:34,640 The next letter is a T and so on and so on. 70 00:03:34,640 --> 00:03:38,230 So essentially you're comparing every letter out here in s1 71 00:03:38,230 --> 00:03:40,240 with every single letter in s2 until you 72 00:03:40,240 --> 00:03:41,500 find one that matches. 73 00:03:41,500 --> 00:03:43,480 As soon as you do, you exit. 74 00:03:43,480 --> 00:03:45,250 So if you actually run this program 75 00:03:45,250 --> 00:03:49,943 it's going to print out seven times, I think. 76 00:03:49,943 --> 00:03:50,905 We can check it. 77 00:03:56,690 --> 00:03:59,690 1, 2, 3, 4, 5, 6, 7 times. 78 00:04:03,090 --> 00:04:03,705 Nice. 79 00:04:03,705 --> 00:04:05,430 The majority of the people are right. 80 00:04:05,430 --> 00:04:07,650 If you got this wrong, please go back and try 81 00:04:07,650 --> 00:04:09,000 to trace through your program. 82 00:04:09,000 --> 00:04:10,830 That means step by step try to figure out 83 00:04:10,830 --> 00:04:12,810 what values of variables are just 84 00:04:12,810 --> 00:04:14,820 to make sure that you're on the right track 85 00:04:14,820 --> 00:04:16,950 and you understand exactly what's happening. 86 00:04:16,950 --> 00:04:18,500 OK.