Skip to content

Conversation

@jhm9595
Copy link
Owner

@jhm9595 jhm9595 commented Sep 28, 2022

Time Complexity : O(N...)?

digitMap.put(6, "mno");
digitMap.put(7, "pqrs");
digitMap.put(8, "tuv");
digitMap.put(9, "wxyz");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually for this kind of objects we'll make it a static object and construct it within a static class.

The reason is that you'll probably be able to reuse the same object at many places but apparently you don't want to construct it again and again.


int digitsLength = digits.length();

String[] str = new String[digitsLength];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String[] str = new String[digits.length()];

You don't need the variable digitsLength.

digits.length() is very trivial and straightforward.

loop--;
}

return list;
Copy link
Collaborator

@bigelephant29 bigelephant29 Oct 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look into this snippet, I wrote this based on your code:

class Solution {
	public List<String> letterCombinations(String digits) {
		
		List<String> list = new ArrayList<String>();
		
		if(digits.isEmpty()) return list;
		
		Map<Integer, String> digitMap = new HashMap<Integer, String>();
		
		digitMap.put(2, "abc");
		digitMap.put(3, "def");
		digitMap.put(4, "ghi");
		digitMap.put(5, "jkl");
		digitMap.put(6, "mno");
		digitMap.put(7, "pqrs");
		digitMap.put(8, "tuv");
		digitMap.put(9, "wxyz");
		
		int digitsLength = digits.length();
		
		list.add("");
        
		for(int i = 0; i < digits.length(); i++) {
			String words = digitMap.get(Integer.valueOf(String.valueOf(digits.charAt(i))));

			List<String> newList = new ArrayList<String>();
			for(String s: list) {
				for(char c: words.toCharArray()) {
					newList.add(s+c);
				}
			}
			list = newList;
		}

		return list;
	}
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow. This make me overwhelmed.
So brilliant! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants