Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1220bc2

Browse files
authored
Merge pull request #84 from greatmerlin/Added-palindrome-problem
added palindrome problem and solution
2 parents dfc0eb6 + 6d6b0ea commit 1220bc2

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Palindrome with Stack and Queue
2+
3+
## Use Stack and Queue to check whether a String is a palindrome
4+
5+
Hint:
6+
7+
Example word: *dad*
8+
9+
stack: push(d), push(a), push(d) : d, a, d
10+
11+
queue: add(d), add(a), add(d) : d, a, d
12+
13+
#### Examples:
14+
15+
// should return true
16+
System.out.println(checkForPalindrome("abccba"));
17+
// should return true
18+
System.out.println(checkForPalindrome("Was it a car or a cat I saw?"));
19+
// should return true
20+
System.out.println(checkForPalindrome("I did, did I?"));
21+
// should return false
22+
System.out.println(checkForPalindrome("hello"));
23+
// should return true
24+
System.out.println(checkForPalindrome("Don't nod"));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.LinkedList;
2+
3+
public static boolean checkForPalindrome(String string) {
4+
5+
LinkedList<Character> stack = new LinkedList<Character>();
6+
LinkedList<Character> queue = new LinkedList<Character>();
7+
String lowerCase = string.toLowerCase();
8+
9+
for (int i = 0; i < lowerCase.length(); i++) {
10+
char c = lowerCase.charAt(i);
11+
if (c >= 'a' && c <= 'z') {
12+
queue.addLast(c);
13+
stack.push(c);
14+
}
15+
}
16+
17+
while (!stack.isEmpty()) {
18+
if (!stack.pop().equals(queue.removeFirst())) {
19+
return false;
20+
}
21+
}
22+
23+
return true;
24+
}
25+
}

‎Practice Problems/Divide_the_chockolate/README.md

Whitespace-only changes.

‎Practice Problems/Divide_the_chockolate/solution.java

Whitespace-only changes.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /