1
00:00:00,110 --> 00:00:00,980
Welcome back.

2
00:00:00,980 --> 00:00:07,640
Let's now implement the code for the tabulation approach where we are just using a one dimensional DP

3
00:00:07,640 --> 00:00:07,910
array.

4
00:00:07,940 --> 00:00:08,450
Okay.

5
00:00:08,450 --> 00:00:13,580
So over here I've written this code already which is something that we have written multiple times.

6
00:00:13,580 --> 00:00:18,920
And over here we have this ace palindrome DP table which is an n into n table.

7
00:00:18,920 --> 00:00:24,470
And in this DP table we have stored whether every substring is a palindrome or not okay.

8
00:00:24,470 --> 00:00:28,910
So I'm not going to again revisit this because we have already done this multiple number of times.

9
00:00:28,910 --> 00:00:31,280
But let's now proceed with the remaining part.

10
00:00:31,280 --> 00:00:35,930
So we are going to have a DP table which is just going to be a one dimensional array.

11
00:00:35,930 --> 00:00:41,540
And this array will be of size n, where n is the size of the input string which is given to us.

12
00:00:41,540 --> 00:00:45,500
And we will fill every cell in this DP table with the value zero.

13
00:00:46,100 --> 00:00:46,460
Okay.

14
00:00:46,460 --> 00:00:49,940
Now what we are going to do is we are going to have a variable.

15
00:00:49,940 --> 00:00:54,980
And using that variable we will iterate over every index in this particular DP table.

16
00:00:54,980 --> 00:00:56,900
Now let me quickly pull up my pen.

17
00:00:56,900 --> 00:01:00,380
So let's say this is the input array which is a b a c.

18
00:01:00,410 --> 00:01:03,620
So over here we have created this particular DP table.

19
00:01:03,620 --> 00:01:06,200
Now I'm going to use this end variable.

20
00:01:06,200 --> 00:01:09,950
And end will take the values from zero up to n minus one.

21
00:01:09,950 --> 00:01:17,630
And for each value of end okay I will have another for loop where the start variable will go from zero

22
00:01:17,630 --> 00:01:24,170
to end, and I will keep checking whether the substring that starts at start and goes up to end is a

23
00:01:24,170 --> 00:01:25,520
palindrome or not.

24
00:01:25,520 --> 00:01:29,510
Okay, and using this I will be able to find the minimum cuts required.

25
00:01:29,510 --> 00:01:29,810
Okay.

26
00:01:29,810 --> 00:01:36,110
So in this DP table over here at every cell, I will be filling the number of cuts or the minimum number

27
00:01:36,110 --> 00:01:41,480
of cuts required for the substring that goes from zero up to that particular index.

28
00:01:41,480 --> 00:01:46,940
For example, over here I will be filling the minimum cuts required for this substring over here.

29
00:01:46,940 --> 00:01:52,310
Similarly, over here I will be filling the minimum number of cuts required for the complete substring.

30
00:01:52,310 --> 00:01:56,210
And that's why this is the cell that ultimately we will be returning.

31
00:01:56,210 --> 00:01:59,390
Now quickly reviewing what we have discussed in the previous video.

32
00:01:59,390 --> 00:02:02,960
Now let's say we have a string which is like b c a.

33
00:02:03,110 --> 00:02:08,810
And we see that this over here is a palindrome where start is over here and end is over here.

34
00:02:08,810 --> 00:02:09,230
Okay.

35
00:02:09,230 --> 00:02:16,220
So if this is the case then the minimum number of cuts required would be one which is this cut over

36
00:02:16,220 --> 00:02:20,210
here plus zero because this over here is a palindrome.

37
00:02:20,210 --> 00:02:25,010
Plus whatever the number of cuts are required for this particular substring.

38
00:02:25,010 --> 00:02:27,620
And this would be something that we have already solved.

39
00:02:27,620 --> 00:02:27,950
Okay.

40
00:02:27,950 --> 00:02:33,680
So if this is start this over here would be obtained by Depee at start minus one.

41
00:02:33,680 --> 00:02:38,570
Because we have discussed that over here in this DP table, at every cell, we are going to fill the

42
00:02:38,570 --> 00:02:44,120
number of cuts that are required for the substring that goes from zero up to that particular index.

43
00:02:44,120 --> 00:02:44,390
Okay.

44
00:02:44,390 --> 00:02:46,760
So this index over here is start minus one.

45
00:02:46,760 --> 00:02:52,400
And that's why DP at start minus one will give us the number of cuts required for this substring.

46
00:02:52,400 --> 00:02:54,800
And then we have one cut over here that is plus one.

47
00:02:54,800 --> 00:02:59,210
And over here because we have identified a palindrome we just need zero cuts.

48
00:02:59,210 --> 00:03:01,370
So this is the approach that we are going to take.

49
00:03:01,370 --> 00:03:03,620
Now let's go ahead and implement this.

50
00:03:07,250 --> 00:03:15,620
So we can say for let end is equal to zero and n less than n n plus plus.

51
00:03:15,620 --> 00:03:16,010
Okay.

52
00:03:16,010 --> 00:03:22,790
Because as we have discussed, n will take all the indices of the DP array and for each value of end

53
00:03:22,790 --> 00:03:29,480
first we will have to set min cuts as the maximum possible number of cuts required, which would be

54
00:03:29,480 --> 00:03:30,320
end itself.

55
00:03:30,320 --> 00:03:30,830
Okay.

56
00:03:31,010 --> 00:03:35,720
For example, let's say in this example over here, we are over here where n is equal to two.

57
00:03:35,750 --> 00:03:38,690
Now in this case the maximum number of cuts would be two.

58
00:03:38,690 --> 00:03:39,140
Right.

59
00:03:39,140 --> 00:03:40,520
Why is that the case.

60
00:03:41,390 --> 00:03:43,460
Because over here we would have to make one cut.

61
00:03:43,460 --> 00:03:45,200
And then you would have to make one cut over here.

62
00:03:45,200 --> 00:03:49,100
And all of these are now single character strings and they are all palindromes.

63
00:03:49,100 --> 00:03:52,970
So that's why the maximum number of cuts is given by n itself.

64
00:03:52,970 --> 00:03:53,330
Okay.

65
00:03:53,330 --> 00:03:56,600
So that's why we can say min cuts is equal to n over here.

66
00:03:56,600 --> 00:04:02,420
And after this we are going to see whether we can identify a value lower than this okay.

67
00:04:02,420 --> 00:04:04,580
And for this we will have another loop.

68
00:04:04,580 --> 00:04:14,330
So let's say for let start is equal to zero and start less than or equal to end start plus plus okay.

69
00:04:14,780 --> 00:04:18,080
Now over here we have to keep moving.

70
00:04:18,080 --> 00:04:24,440
Start till we get a scenario where the substring that goes from start to end is a palindrome.

71
00:04:24,440 --> 00:04:28,190
So I can say if is palindrome start end.

72
00:04:28,610 --> 00:04:34,460
So if we get this type of a scenario then first we will check whether start is equal to zero.

73
00:04:34,460 --> 00:04:39,320
Because if start is equal to zero, then the entire substring is a palindrome.

74
00:04:39,320 --> 00:04:41,810
And therefore we would not require any cuts.

75
00:04:41,810 --> 00:04:44,840
So in that case min cuts can be made zero.

76
00:04:44,840 --> 00:04:47,510
But if that is not the case okay.

77
00:04:47,510 --> 00:04:49,460
So let's just move this a little bit up.

78
00:04:49,880 --> 00:04:51,380
So if this is not the case.

79
00:04:51,380 --> 00:04:52,850
So let's have an else over here.

80
00:04:52,850 --> 00:05:01,010
So if this is not the case then min cuts will be the minimum between what we have already in min cuts

81
00:05:01,010 --> 00:05:07,460
and one plus DP at start minus one okay.

82
00:05:07,460 --> 00:05:10,370
And we have discussed why this is the case in detail okay.

83
00:05:10,370 --> 00:05:12,650
So this would be the minimum cuts required.

84
00:05:12,650 --> 00:05:19,310
And then once we are out of this for loop over here that is over here we will just say DP at end because

85
00:05:19,310 --> 00:05:21,770
this has been done for a particular value of n right.

86
00:05:21,770 --> 00:05:23,180
Which is what we get over here.

87
00:05:23,180 --> 00:05:26,750
So we can just say dp at end is equal to min cuts.

88
00:05:27,050 --> 00:05:27,650
That's it.

89
00:05:27,650 --> 00:05:33,410
And then once we are out of this for loop we will just have to return dp at n minus one.

90
00:05:33,800 --> 00:05:34,190
Right.

91
00:05:34,190 --> 00:05:39,830
Because we have discussed at every index in the DP array, we are going to store the minimum number

92
00:05:39,830 --> 00:05:46,340
of cuts required for the substring that spans from zero up to that particular index and dp at n minus

93
00:05:46,340 --> 00:05:46,580
one.

94
00:05:46,580 --> 00:05:51,650
In this regard, would have the minimum number of cuts required for the complete string, right?

95
00:05:51,650 --> 00:05:53,930
The minimum cuts required for the entire string.

96
00:05:53,930 --> 00:05:57,110
So that is the answer to the question which has been asked to us.

97
00:05:57,110 --> 00:05:58,730
And therefore we return this.

98
00:05:58,730 --> 00:06:04,580
So this is the tabulation approach for solving palindrome partitioning to by just using a one dimensional

99
00:06:04,580 --> 00:06:05,090
DP array.

100
00:06:05,120 --> 00:06:08,960
Now let's go ahead and run this code and see if it's passing all the test cases.

101
00:06:09,710 --> 00:06:11,360
And you can see that it's working.

102
00:06:11,360 --> 00:06:13,310
It's passing all the test cases.
