enh(Go): import the string package on anagram (#838)
parent
7f85abfa31
commit
daa8be12a4
|
@ -1,29 +1,23 @@
|
||||||
package main
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Problem Statement: Given two strings s and t, find they are anagrams or not
|
Problem Statement: Given two strings s and t, find they are anagrams or not
|
||||||
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
|
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
|
||||||
|
|
||||||
**/
|
|
||||||
|
|
||||||
/***
|
|
||||||
Example 1: Input: s="anagram" t="nagaram"
|
Example 1: Input: s="anagram" t="nagaram"
|
||||||
Output: true
|
Output: true
|
||||||
|
|
||||||
Example 2: Input: s="rat" t="car"
|
Example 2: Input: s="rat" t="car"
|
||||||
Output: false
|
Output: false
|
||||||
|
|
||||||
***/
|
|
||||||
|
|
||||||
/***
|
|
||||||
Time complexity: O(n), n=length of string s
|
Time complexity: O(n), n=length of string s
|
||||||
Space complexity: O(1)
|
Space complexity: O(1)
|
||||||
|
|
||||||
**/
|
**/
|
||||||
|
|
||||||
|
package strings
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func RunIsAnagram() {
|
||||||
fmt.Println("Enter first string")
|
fmt.Println("Enter first string")
|
||||||
var first string
|
var first string
|
||||||
fmt.Scanln(&first)
|
fmt.Scanln(&first)
|
||||||
|
@ -37,9 +31,10 @@ func main() {
|
||||||
fmt.Println("They are not anagrams")
|
fmt.Println("They are not anagrams")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func isAnagram(s string, t string) bool {
|
func isAnagram(s string, t string) bool {
|
||||||
var count [256]int
|
var count [256]int
|
||||||
//var i int
|
|
||||||
for i := 0; i < len(s); i++ {
|
for i := 0; i < len(s); i++ {
|
||||||
count[s[i]]++
|
count[s[i]]++
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,10 @@
|
||||||
|
/*
|
||||||
|
Given a string s, return true if we can have a palindrome from the permutation of the input
|
||||||
|
A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar.
|
||||||
|
|
||||||
|
Time: O(n)
|
||||||
|
Space: O(n)
|
||||||
|
*/
|
||||||
package strings
|
package strings
|
||||||
|
|
||||||
import(
|
import(
|
||||||
|
@ -5,12 +12,7 @@ import(
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
|
||||||
Given a string s, return true if we can have a palindrome from the permutation of the input
|
|
||||||
|
|
||||||
Time: O(n)
|
|
||||||
Space: O(n)
|
|
||||||
*/
|
|
||||||
func canPermutePalindrome(s string) bool {
|
func canPermutePalindrome(s string) bool {
|
||||||
a := strings.Split(s,"")
|
a := strings.Split(s,"")
|
||||||
dictionary := make(map[string] int)
|
dictionary := make(map[string] int)
|
||||||
|
@ -29,7 +31,7 @@ func canPermutePalindrome(s string) bool {
|
||||||
|
|
||||||
|
|
||||||
//You are welcome to play around with the test cases
|
//You are welcome to play around with the test cases
|
||||||
func runPermutationCheck(){
|
func RunPermutationCheck(){
|
||||||
input1 := "carerac"
|
input1 := "carerac"
|
||||||
|
|
||||||
fmt.Printf("%t for input %s \n", canPermutePalindrome(input1), input1) //should print true
|
fmt.Printf("%t for input %s \n", canPermutePalindrome(input1), input1) //should print true
|
||||||
|
|
Loading…
Reference in New Issue