Sit-in lab 4. Given a number of strings containing only “a”, “b”, “c”, “d” Check...
-
Upload
randolph-simon -
Category
Documents
-
view
217 -
download
4
Transcript of Sit-in lab 4. Given a number of strings containing only “a”, “b”, “c”, “d” Check...
![Page 1: Sit-in lab 4. Given a number of strings containing only “a”, “b”, “c”, “d” Check each string that it follows the pattern a n b m c m d n where m,](https://reader035.fdocuments.in/reader035/viewer/2022071806/56649d055503460f949d90f3/html5/thumbnails/1.jpg)
String Checker IISit-in lab 4
![Page 2: Sit-in lab 4. Given a number of strings containing only “a”, “b”, “c”, “d” Check each string that it follows the pattern a n b m c m d n where m,](https://reader035.fdocuments.in/reader035/viewer/2022071806/56649d055503460f949d90f3/html5/thumbnails/2.jpg)
Given a number of strings containing only “a”, “b”, “c”, “d”
Check each string that it follows the pattern anbmcmdn where m, n >= 1
Use only stack OR queue
Problem Description
![Page 3: Sit-in lab 4. Given a number of strings containing only “a”, “b”, “c”, “d” Check each string that it follows the pattern a n b m c m d n where m,](https://reader035.fdocuments.in/reader035/viewer/2022071806/56649d055503460f949d90f3/html5/thumbnails/3.jpg)
Look at pattern: anbmcmdn
Notice that it is LIFO So we use a Stack!
Many ways of solving with a stack… here we show one way
Solution
![Page 4: Sit-in lab 4. Given a number of strings containing only “a”, “b”, “c”, “d” Check each string that it follows the pattern a n b m c m d n where m,](https://reader035.fdocuments.in/reader035/viewer/2022071806/56649d055503460f949d90f3/html5/thumbnails/4.jpg)
Process one character from the input String at a time.
For example: for(int j = 0; j < s.length(); j++){
strCheck.nextChar(s.charAt(j));}
Solution
![Page 5: Sit-in lab 4. Given a number of strings containing only “a”, “b”, “c”, “d” Check each string that it follows the pattern a n b m c m d n where m,](https://reader035.fdocuments.in/reader035/viewer/2022071806/56649d055503460f949d90f3/html5/thumbnails/5.jpg)
If character is “a”◦ We push to stack if
the stack is empty, or the top of the stack is “a”
◦ Else is not valid string
If character is “b”◦ Remember that we have a “b” (set a boolean flag)◦ We push to stack if
the stack is NOT empty, and the top of the stack is “a” or “b”
◦ Else is not valid string
Solution
We need to check this so that things like “bc” will be caught
![Page 6: Sit-in lab 4. Given a number of strings containing only “a”, “b”, “c”, “d” Check each string that it follows the pattern a n b m c m d n where m,](https://reader035.fdocuments.in/reader035/viewer/2022071806/56649d055503460f949d90f3/html5/thumbnails/6.jpg)
If character is “c”◦ We pop from the stack if
the stack is NOT empty, and the top of the stack is “b”
◦ Else is not valid string
If character is “d”◦ We pop from the stack if
the stack is NOT empty, and the top of the stack is “a”, and there was a “b” read in (special check for things like “ad”)
◦ Else is not valid string
Solution
![Page 7: Sit-in lab 4. Given a number of strings containing only “a”, “b”, “c”, “d” Check each string that it follows the pattern a n b m c m d n where m,](https://reader035.fdocuments.in/reader035/viewer/2022071806/56649d055503460f949d90f3/html5/thumbnails/7.jpg)
Finally, before concluding is valid string, check that stack is empty◦ We need this to catch things like “aaaaaabbccd”
(there will be many “a” left behind in the stack)
Solution
![Page 8: Sit-in lab 4. Given a number of strings containing only “a”, “b”, “c”, “d” Check each string that it follows the pattern a n b m c m d n where m,](https://reader035.fdocuments.in/reader035/viewer/2022071806/56649d055503460f949d90f3/html5/thumbnails/8.jpg)
End
![Page 9: Sit-in lab 4. Given a number of strings containing only “a”, “b”, “c”, “d” Check each string that it follows the pattern a n b m c m d n where m,](https://reader035.fdocuments.in/reader035/viewer/2022071806/56649d055503460f949d90f3/html5/thumbnails/9.jpg)
String Checker I