Skip to content

Commit b16915b

Browse files
committed
pushing changes for 08/13
1 parent 8249ae4 commit b16915b

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Feel free to add issues, comment and pull request.
4444
| Leetcode | [557. Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/) | [Java](./java/reverseWords.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
4545
| Leetcode | [387. First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [Java](./java/FirstUniqChar.java) \| [Python](./Python/) | _O(n)_ | _O(n)_ | Easy | |
4646
| Leetcode | [647. Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [Java](./java/CountSubstrings.java) \| [Python](./Python/) | _O(n^2)_ | _O(1)_ | Easy | |
47+
| Leetcode | [500. Keyboard Row](https://leetcode.com/problems/keyboard-row/description/) | [Java](./java/findWords.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
4748

4849
## HashMap
4950
| Website | Title | Solution | Time | Space | Difficulty | Note|

java/findWords.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class Solution {
2+
3+
// Used String datastructure to store the rows
4+
private String [] rows = new String [] {"QWERTYUIOP","ASDFGHJKL","ZXCVBNM"};
5+
6+
public String[] findWords(String[] words) {
7+
8+
ArrayList<String> findWord = new ArrayList<>();
9+
int rowNumber = -1;
10+
11+
for(String word : words)
12+
{
13+
char [] charWord = word.toCharArray();
14+
rowNumber = findRowNumber(charWord[0]);
15+
for(int idx = 1; idx<charWord.length; idx++)
16+
{
17+
// If the rows are different, then update rowNumber as -1
18+
if(rowNumber != findRowNumber(charWord[idx]))
19+
{
20+
rowNumber = -1;
21+
break;
22+
}
23+
}
24+
25+
if(rowNumber != -1)
26+
findWord.add(new String(charWord));
27+
}
28+
return findWord.toArray(new String[findWord.size()]);
29+
}
30+
31+
public int findRowNumber(char c)
32+
{
33+
for(int num=0; num<3; num++)
34+
{ //Identify the row number
35+
if(rows[num].indexOf(Character.toUpperCase(c)) != -1)
36+
return num;
37+
}
38+
return -1;
39+
}
40+
}

java/shouldPrintMessage.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Logger {
2+
3+
private HashMap<String, Integer> map;
4+
5+
/** Initialize your data structure here. */
6+
public Logger() {
7+
map = new HashMap<>();
8+
9+
}
10+
11+
/** Returns true if the message should be printed in the given timestamp, otherwise returns false.
12+
If this method returns false, the message will not be printed.
13+
The timestamp is in seconds granularity. */
14+
public boolean shouldPrintMessage(int timestamp, String message) {
15+
if(!map.containsKey(message) || timestamp - map.get(message) >= 10)
16+
{
17+
map.put(message, timestamp);
18+
return true;
19+
}
20+
21+
return false;
22+
23+
}
24+
}
25+
26+
/**
27+
* Your Logger object will be instantiated and called as such:
28+
* Logger obj = new Logger();
29+
* boolean param_1 = obj.shouldPrintMessage(timestamp,message);
30+
*/

0 commit comments

Comments
 (0)